programing

WordPress 및 Ajax - 쇼트코드 콘텐츠 새로고침

showcode 2023. 4. 5. 22:30
반응형

WordPress 및 Ajax - 쇼트코드 콘텐츠 새로고침

커스텀 플러그인에서는,demoConnectors쇼트 코드와 초기화를 실시합니다.이것은 PHP 변수를 포함하고 있습니다.input of type select따라서 사용자는 파라미터를 선택해야 하며 PHP 변수는 Ajax를 통해 업데이트됩니다.선택한 파라미터에 따라 쇼트코드의 내용이 변경됩니다.

문제는 Ajax가 트리거된 후 쇼트코드 콘텐츠를 업데이트하는 방법을 모른다는 것입니다.

제 PHP 코드는 다음과 같습니다.

<?php 
/**
 *   Plugin Name: demoConnecteurs
 *   Description: Plugin de démo des connecteurs Jenkins et Mantis
**/
require_once(file_with_external_fonctions.php);

$inst_demoConnecteurs = new demoConnecteurs();
if (isset($inst_demoConnecteurs)){
}

class demoConnecteurs{   
    private $projects;
    private $versions;

    private $project_id;
    private $project_name;
    private $version_id;


    function __construct(){
        $this->setProjects();

        $this->initAjaxActions();

        add_action('admin_enqueue_scripts', array($this,'demo_scripts'));
        add_action('wp_enqueue_scripts', array($this,'demo_scripts'));


        $this->init();
    }

    function initAjaxActions(){
        add_action('wp_ajax_setProjectChosen', array($this,'setProjectChosen'));
        add_action('wp_ajax_nopriv_setProjectChosen', array($this,'setProjectChosen'));
    }

    function demo_scripts(){
        wp_register_script( 'ajaxHandle', plugins_url() . '/DemoConnecteurs/buttons_ajax.js');
        wp_localize_script( 'ajaxHandle', 'ajax_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );

        wp_enqueue_script( 'ajaxHandle');
    }

    function init(){
        add_shortcode( 'demoConnecteurs', array($this,'demoConnecteurs_shortcode') );
    }

    function demoConnecteurs_shortcode () {
        return $this->contentDemoConnecteurs();
    }

    public function setProjects(){
        $this->projects = getProjects();
    }

    public function setProjectChosen(){
        if (isset ($_POST['demo_projet_name']))$this->project_name = $_POST['demo_projet_name'];
        $this->project_id = getProjectIdFromName($this->mantisClient,  $_SESSION['username'], $_SESSION['password'], $this->project_name);

        $this->setVersions();
        echo $this->contentDemoConnecteurs();
        wp_die();
    }

    public function setVersions(){
        $this->versions = getVersionsOfProjectChosen($this->project_id);
    }

    function contentDemoConnecteurs(){
        $html = "";

        $html .= 'Choix du projet : ';        
        $html .= '<select id="projectChosen" name="project">';
        foreach($this->projects as $p) {
            $selected = ($p->id == $this->project_id) ? 'selected="selected"' : '';
            $html .= '<option value="' . $p->name .'" '. $selected . ' >' . $p->name . '</option>';
        }
        $html .= '</select><br>';

        $html .= 'Choix de la version : ';
        if (isset ($this->versions)){
            $html .= '<select id="versionChosen" name="version">';
            foreach($this->versions as $v) {
                $selected = ($v->id == $this->version_id) ? 'selected="selected"' : '';
                $html .= '<option value="' . $v->id .'" '. $selected . ' >' . $v->id . '</option>';
            }
            $html .= '</select>';
        }

        return $html;
    }
}

여기 jQuery 코드가 있습니다.

jQuery(document).ready(function($) {

    $('#projectChosen').on('change', function () {
        jQuery.ajax({
            type: "POST",
            url: ajax_object.ajaxurl,
            data: {
                'action': 'setProjectChosen',
                'demo_projet_name': $('#projectChosen option:selected').val()
            },
            success: function (output) {
                //how can I update the content of my shortcode with my variable output 
            },
            error: function(errorThrown){
                console.log(errorThrown);
            }
        });
    } );
} );

편집

필터를 사용하려고 합니다.do_shortcode_tag쇼트 코드의 내용을 갱신할 수 없습니다.내용이 갱신되지 않을 뿐입니다.

public function setProjectChosen(){
        if (isset ($_POST['demo_projet_name']))$this->project_name = $_POST['demo_projet_name'];
        $this->project_id = getProjectIdFromName($this->mantisClient,  $_SESSION['username'], $_SESSION['password'], $this->project_name);

        $this->setVersions();

        apply_filters( 'do_shortcode_tag', array($this, 'contentDemoConnecteurs'), 'demoConnecteurs',10,3 );

        wp_die();
    }

코멘트를 쓰고 싶지만, 지금의 평판에서는 대답밖에 쓸 수 없습니다.AJAX 출력의 내용을 재인쇄하기 위한 솔루션을 여기에 나타냅니다.

PHP에서 ID가 다음과 같은 컨테이너 div를 추가합니다.

function contentDemoConnecteurs(){
    $html = '<div id="projectSelector">';

    $html .= 'Choix du projet : ';        
    $html .= '<select id="projectChosen" name="project">';
    foreach($this->projects as $p) {
        $selected = ($p->id == $this->project_id) ? 'selected="selected"' : '';
        $html .= '<option value="' . $p->name .'" '. $selected . ' >' . $p->name . '</option>';
    }
    $html .= '</select><br>';

    $html .= 'Choix de la version : ';
    if (isset ($this->versions)){
        $html .= '<select id="versionChosen" name="version">';
        foreach($this->versions as $v) {
            $selected = ($v->id == $this->version_id) ? 'selected="selected"' : '';
            $html .= '<option value="' . $v->id .'" '. $selected . ' >' . $v->id . '</option>';
        }
        $html .= '</select>';
    }
    $html .= '</div>';
    return $html;
}

JQuery의 경우:

jQuery(document).ready(function($) {
    $('#projectChosen').on('change', function () {
        jQuery.ajax({
            type: "POST",
            url: ajax_object.ajaxurl,
            data: {
                'action': 'setProjectChosen',
                'demo_projet_name': $('#projectChosen option:selected').val()
            },
            success: function (output) {
                $( "div#projectSelector" ).replaceWith(output);
            },
            error: function(errorThrown){
                console.log(errorThrown);
            }
        });
    } );
} );

그게 네게 필요한 것이길 바라.

드디어 해냈는데 뭘 하고 싶은지 헷갈려서...

<?php 
/**
 *   Plugin Name: demoConnecteurs
 *   Description: Plugin de démo des connecteurs Jenkins et Mantis
**/
require_once(file_with_external_fonctions.php);

$inst_demoConnecteurs = new demoConnecteurs();
if (isset($inst_demoConnecteurs)){
}

class demoConnecteurs{   
    private $projects;
    private $versions;

    private $project_id;
    private $project_name;
    private $version_id;


    function __construct(){
        $this->setProjects();

        $this->initAjaxActions();

        add_action('admin_enqueue_scripts', array($this,'demo_scripts'));
        add_action('wp_enqueue_scripts', array($this,'demo_scripts'));


        $this->init();
    }

    function initAjaxActions(){
        add_action('wp_ajax_setProjectChosen', array($this,'setProjectChosen'));
        add_action('wp_ajax_nopriv_setProjectChosen', array($this,'setProjectChosen'));
    }

    function demo_scripts(){
        wp_register_script( 'ajaxHandle', plugins_url() . '/DemoConnecteurs/buttons_ajax.js');
        wp_localize_script( 'ajaxHandle', 'ajax_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );

        wp_enqueue_script( 'ajaxHandle');
    }

    function init(){
        add_shortcode( 'demoConnecteurs', array($this,'demoConnecteurs_shortcode') );
    }

    function demoConnecteurs_shortcode () {
        return $this->contentDemoConnecteurs();
    }

    public function setProjects(){
        $this->projects = getProjects();
    }

    public function setProjectChosen(){
        if (isset ($_POST['demo_projet_name']))$this->project_name = $_POST['demo_projet_name'];
        $this->project_id = getProjectIdFromName($this->mantisClient,  $_SESSION['username'], $_SESSION['password'], $this->project_name);

        $this->setVersions();
        echo $this->contentDemoConnecteurs();
        wp_die();
    }

    public function setVersions(){
        $this->versions = getVersionsOfProjectChosen($this->project_id);
    }

    function contentDemoConnecteurs(){
        $html = '<div id="contentDemoConnecteurs">';

        $html .= 'Choix du projet : ';        
        $html .= '<select id="projectChosen" name="project">';
        foreach($this->projects as $p) {
            $selected = ($p->id == $this->project_id) ? 'selected="selected"' : '';
            $html .= '<option value="' . $p->name .'" '. $selected . ' >' . $p->name . '</option>';
        }
        $html .= '</select><br>';

        $html .= 'Choix de la version : ';
        if (isset ($this->versions)){
            $html .= '<select id="versionChosen" name="version">';
            foreach($this->versions as $v) {
                $selected = ($v->id == $this->version_id) ? 'selected="selected"' : '';
                $html .= '<option value="' . $v->id .'" '. $selected . ' >' . $v->id . '</option>';
            }
            $html .= '</select>';
        }
        $html .= '</div>';
        return $html;
    }
}
jQuery(document).ready(function($) {

    $('#projectChosen').on('change', function () {
        jQuery.ajax({
            type: "POST",
            url: ajax_object.ajaxurl,
            data: {
                'action': 'setProjectChosen',
                'demo_projet_name': $('#projectChosen option:selected').val()
            },
            success: function (output) {
                $('#contentDemoConnecteurs').replaceWith(output);
            },
            error: function(errorThrown){
                console.log(errorThrown);
            }
        });
    } );
} );

언급URL : https://stackoverflow.com/questions/56601158/wordpress-and-ajax-reload-shortcode-content

반응형