亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

單擊按鈕時執行短代碼

單擊按鈕時執行短代碼

PHP
森林海 2023-08-11 17:41:17
無法讓它工作。到目前為止,這是我的代碼:jQuery? ? jQuery(document).ready(function($) {? ? ? ? $('#extra').on('click',function() {? ? ? ? ? ? $.ajax({? ? ? ? ? ? ? ? type: "POST",?? ? ? ? ? ? ? ? url: my_ajaxurl,?? ? ? ? ? ? ? ? data: {? ? ? ? ? ? ? ? ? ? action : 'process_shortcode_on_click_action'? ? ? ? ? ? ? ? },? ? ? ? ? ? ? ? success:function(data) {? ? ? ? ? ? ? ? ? ? console.log("Success");? ? ? ? ? ? ? ? },? ? ? ? ? ? ? ? error: function(errorThrown){? ? ? ? ? ? ? ? ? ? console.log("Error");? ? ? ? ? ? ? ? }? ? ? ? ? ? });? ? ? ? })? ? })函數.php? ? add_action( 'wp_enqueue_scripts', 'add_my_script' );? ? function add_my_script() {? ? ? ? wp_enqueue_script(? ? ? ? ? ? 'extra-script', // name your script so that you can attach other scripts and de-register, etc.? ? ? ? ? ? get_template_directory_uri() . '/js/script.js', // this is the location of your script file? ? ? ? ? ? array('jquery') // this array lists the scripts upon which your script depends? ? ? ? );? ? ? ? wp_localize_script( 'extra-script', 'my_ajaxurl', admin_url( 'admin-ajax.php' ) );? ? }? ??? ? add_shortcode( 'extra',? 't5_extra_content' );? ? add_action( 'add_meta_boxes_post', 't5_register_extra_metabox' );? ? add_action( 'save_post', 't5_save_shortcode_box', 10, 2);? ? add_action( 'wp_ajax_process_shortcode_on_click_action', 'process_shortcode_on_click_ajax');? ? add_action( 'wp_ajax_nopriv_process_shortcode_on_click_action', 'process_shortcode_on_click_ajax');? ??? ? function process_shortcode_on_click_ajax() {? ? ? ? echo do_shortcode('[extra]');? ? ? ? die;? ? }我在控制臺中看到“成功”,所以我知道 jQuery 被正確調用。然而,我不知道如何讓 do_shortcode('[extra]') 觸發。任何幫助是極大的贊賞。
查看完整描述

1 回答

?
互換的青春

TA貢獻1797條經驗 獲得超6個贊

您的 ajax 調用沒有$post來自 ajax 請求的頁面的全局內容,因此get_the_ID()應該是 false 并且get_post_meta( get_the_ID(), '_t5_extra_box', TRUE )也將是 false。此外,您正在調用do_shortcode('[extra]')(無內容),因此$content回調內將是一個空字符串。所以return wpautop(get_post_meta( get_the_ID(), '_t5_extra_box', TRUE).$content);就變成了return wpautop('');一個空字符串。根據您的代碼,我希望您的data響應始終為空字符串。

為了解決這個問題,我將添加一個額外的 ajax post 數據項以及告訴回調$post_id應該是什么的操作。這是最原始的方式。您可能想要添加隨機數或其他一些安全措施(但這可能與您原來的問題無關)。

更新

我還沒有對此進行測試,您可能想要以不同的方式進行操作,但這里有一個選擇。最終,您只需要一種方法來從原始請求中獲取$post_id并將其傳遞給 ajax 調用。由于您已經通過 ajax url 傳遞wp_localize_script,我只需在那里添加另一個 JavaScript 變量。

jQuery

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

    $('#extra').on('click', function () {

        $.ajax({

            type: "POST",

            url: my_ajaxurl,

            data: {

                action: 'process_shortcode_on_click_action',

                post_id: my_postid,

            },

            success: function (data) {

                console.log("Success");

            },

            error: function (errorThrown) {

                console.log("Error");

            }

        });

    })

})

函數.php


add_action('wp_enqueue_scripts', 'add_my_script');

function add_my_script () {

    wp_enqueue_script(

        'extra-script', // name your script so that you can attach other scripts and de-register, etc.

        get_template_directory_uri() . '/js/script.js', // this is the location of your script file

        array('jquery') // this array lists the scripts upon which your script depends

    );

    wp_localize_script('extra-script', 'my_ajaxurl', admin_url('admin-ajax.php'));

    wp_localize_script('extra-script', 'my_postid', get_the_ID());

}


add_action('wp_ajax_process_shortcode_on_click_action', 'process_shortcode_on_click_ajax');

add_action('wp_ajax_nopriv_process_shortcode_on_click_action', 'process_shortcode_on_click_ajax');

function process_shortcode_on_click_ajax ()

{

    $_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);


    if (empty($post_id = $_POST['post_id']) || !is_numeric($post_id)) {

        wp_die('Post ID is Invalid', 400);

    }


    echo do_shortcode("[extra post_id='{$post_id}']");

    wp_die();

}


add_shortcode('extra', 't5_extra_content');

function t5_extra_content ($attributes, $content = '')

{

    $defaults = [

        'post_id' => get_the_ID(),

        'cap'     => 'edit_posts'

    ];


    $args = shortcode_atts($defaults, $attributes);


    if (!current_user_can($args['cap']) || empty($args['post_id'])) {

        return ''; // or some message on fail

    }


    return wpautop(get_post_meta($args['post_id'], '_t5_extra_box', TRUE) . $content);

}


查看完整回答
反對 回復 2023-08-11
  • 1 回答
  • 0 關注
  • 119 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號