我有一个插件作为一个类,其中我使用一个shortcode属性来填充$outlet变量。然而,尽管sanitize_text_field($atts[\'outlet\'])
返回所需字符串,$this->outlet = sanitize_text_field($atts[\'outlet\'])
未更新的值$outlet
. 如果我为类的$outlet
最初,它在example_callback()
. 插件的所有其他功能都按预期工作。只是wp_news_shortcode()
未分配$attr
价值
总之,我需要在ajax_callback()
功能是否有我忽略的东西?
class Example _Search {
private static $instance = null;
public $outlet;
public function __construct() {
$this->settings = array(
\'plugin_path\' => plugin_dir_path(__FILE__),
\'plugin_url\' => plugin_dir_url(__FILE__),
\'plugin_base\' => dirname(plugin_basename(__FILE__)),
\'plugin_base_url\' => plugin_basename(__FILE__),
\'plugin_file\' => __FILE__,
\'plugin_version\' => \'1.0.0\',
);
$this->run_plugin();
}
/**
* Singleton.
*
* @return self Main instance.
*/
public static function init() {
if (is_null(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Main plugin function.
*
* @since 1.0.0
*/
public function run_plugin() {
add_action(\'wp_ajax_example_callback\', array($this, \'example_callback\'));
add_action(\'wp_ajax_nopriv_example_callback\', array($this, \'example_callback\'));
add_shortcode(\'wp_example_search\', array($this, \'wp_example_shortcode\'));
}
/**
* Example Search shortcode. Adds form to page and stores "outlet" attribute to options.
*
* @since 1.0.0
* @param array $atts An associative array of attributes.
* @return string
*/
public function wp_example_shortcode($atts) {
$atts = shortcode_atts(
array(
\'outlet\' => \'google\',
),
$atts
);
$this->outlet = sanitize_text_field($atts[\'outlet\']);
$content = \'\';
$view = $this->get_view_path(\'form.php\');
if (file_exists($view)) {
ob_start();
include $view;
$content = ob_get_clean();
}
return $content;
}
/**
* Get the path to view.
*
* @param string $view_name View name.
* @param string|boolean $sub_dir_name The sub-directory.
* @return string
*/
public function get_view_path($view_name, $sub_dir_name = false) {
$path = $rel_path = \'\';
$plugin_base = \'wp-example-search\';
if (!empty($sub_dir_name)) {
$rel_path .= "/{$sub_dir_name}";
}
$rel_path .= "/{$view_name}";
$path = $this->settings[\'plugin_path\'] . \'views\' . $rel_path;
return $path;
}
/**
* Handle AJAX request.
*/
public function example_callback() {
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
if (isset($_POST[\'data\'])) {
$form_data = ($_POST[\'data\']);
$keyword = trim($form_data[\'keyword\']);
$endpoint = \'https://example.com?q=\' . rawurlencode($keyword) . \'&domains=\' .
$this->outlet . \'.com\';
];
$response = wp_remote_get($endpoint);
$response_body = wp_remote_retrieve_body($response);
$result = json_decode($response_body);
$result_items = $result->items;
if (is_array($result) && !is_wp_error($result)) {
$error_message = $result->get_error_message();
echo "Something went wrong: $error_message";
} else {
$content = \'\';
$view = $this->get_view_path(\'article.php\');
if (file_exists($view)) {
ob_start();
include $view;
$content = ob_get_clean();
}
echo $content;
}
}
die();
}
}
Example_Search::init();
最合适的回答,由SO网友:Jacob Peattie 整理而成
这不是PHP的工作方式。PHP类不是持久性的,因此在输出短代码时设置属性不会影响后续的AJAX请求,因为在每个请求上都会重新创建类实例。
如果您有一个短代码,它有一个;“出口”;用户可以这样使用的属性:
[wp_example_search outlet="google"]
该短代码输出一个表单,该表单在提交时运行AJAX请求,您需要AJAX响应来了解“什么是”;“出口”;要使用,那么唯一的方法就是将该信息与AJAX请求一起发送。要做到这一点,短代码需要在一个隐藏字段或属性中输出相关的出口,以便JavaScript可以检索它并将其与请求一起发送。