nin*_*ath 7 php ajax wordpress jquery
在自定义插件中,我生成一个demoConnectors
简码并初始化其内容。此代码包含中的PHP变量input of type select
。因此,用户必须选择参数,并通过ajax更新PHP变量。根据所选参数,可以修改短代码的内容。
问题是在触发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;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的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);
}
});
} );
} );
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用过滤器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();
}
Run Code Online (Sandbox Code Playgroud)
终于成功了,我很困惑我想做什么......
\n\n<?php \n/**\n * Plugin Name: demoConnecteurs\n * Description: Plugin de d\xc3\xa9mo des connecteurs Jenkins et Mantis\n**/\nrequire_once(file_with_external_fonctions.php);\n\n$inst_demoConnecteurs = new demoConnecteurs();\nif (isset($inst_demoConnecteurs)){\n}\n\nclass demoConnecteurs{ \n private $projects;\n private $versions;\n\n private $project_id;\n private $project_name;\n private $version_id;\n\n\n function __construct(){\n $this->setProjects();\n\n $this->initAjaxActions();\n\n add_action(\'admin_enqueue_scripts\', array($this,\'demo_scripts\'));\n add_action(\'wp_enqueue_scripts\', array($this,\'demo_scripts\'));\n\n\n $this->init();\n }\n\n function initAjaxActions(){\n add_action(\'wp_ajax_setProjectChosen\', array($this,\'setProjectChosen\'));\n add_action(\'wp_ajax_nopriv_setProjectChosen\', array($this,\'setProjectChosen\'));\n }\n\n function demo_scripts(){\n wp_register_script( \'ajaxHandle\', plugins_url() . \'/DemoConnecteurs/buttons_ajax.js\');\n wp_localize_script( \'ajaxHandle\', \'ajax_object\', array( \'ajaxurl\' => admin_url( \'admin-ajax.php\' ) ) );\n\n wp_enqueue_script( \'ajaxHandle\');\n }\n\n function init(){\n add_shortcode( \'demoConnecteurs\', array($this,\'demoConnecteurs_shortcode\') );\n }\n\n function demoConnecteurs_shortcode () {\n return $this->contentDemoConnecteurs();\n }\n\n public function setProjects(){\n $this->projects = getProjects();\n }\n\n public function setProjectChosen(){\n if (isset ($_POST[\'demo_projet_name\']))$this->project_name = $_POST[\'demo_projet_name\'];\n $this->project_id = getProjectIdFromName($this->mantisClient, $_SESSION[\'username\'], $_SESSION[\'password\'], $this->project_name);\n\n $this->setVersions();\n echo $this->contentDemoConnecteurs();\n wp_die();\n }\n\n public function setVersions(){\n $this->versions = getVersionsOfProjectChosen($this->project_id);\n }\n\n function contentDemoConnecteurs(){\n $html = \'<div id="contentDemoConnecteurs">\';\n\n $html .= \'Choix du projet : \'; \n $html .= \'<select id="projectChosen" name="project">\';\n foreach($this->projects as $p) {\n $selected = ($p->id == $this->project_id) ? \'selected="selected"\' : \'\';\n $html .= \'<option value="\' . $p->name .\'" \'. $selected . \' >\' . $p->name . \'</option>\';\n }\n $html .= \'</select><br>\';\n\n $html .= \'Choix de la version : \';\n if (isset ($this->versions)){\n $html .= \'<select id="versionChosen" name="version">\';\n foreach($this->versions as $v) {\n $selected = ($v->id == $this->version_id) ? \'selected="selected"\' : \'\';\n $html .= \'<option value="\' . $v->id .\'" \'. $selected . \' >\' . $v->id . \'</option>\';\n }\n $html .= \'</select>\';\n }\n $html .= \'</div>\';\n return $html;\n }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\njQuery(document).ready(function($) {\n\n $(\'#projectChosen\').on(\'change\', function () {\n jQuery.ajax({\n type: "POST",\n url: ajax_object.ajaxurl,\n data: {\n \'action\': \'setProjectChosen\',\n \'demo_projet_name\': $(\'#projectChosen option:selected\').val()\n },\n success: function (output) {\n $(\'#contentDemoConnecteurs\').replaceWith(output);\n },\n error: function(errorThrown){\n console.log(errorThrown);\n }\n });\n } );\n} );\n
Run Code Online (Sandbox Code Playgroud)\n