gpt4 book ai didi

php - WordPress 和 Ajax - 重新加载简码内容

转载 作者:可可西里 更新时间:2023-11-01 12:53:55 28 4
gpt4 key购买 nike

在自定义插件中,我生成了一个 demoConnectors 简码并初始化了它的内容。这一个包含 input of type select 中的 PHP 变量。因此,用户必须选择参数,并且 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 - WordPress 和 Ajax - 重新加载简码内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56601158/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com