【问题标题】:Create category on the front-end with Ajax in WordPress在 WordPress 中使用 Ajax 在前端创建类别
【发布时间】:2019-02-09 00:08:52
【问题描述】:

我很难用 Ajax 在前端创建一个类别。 99% 都在工作。

这是我的表格:

<form id="new_idea" name="new_idea" method="POST">
    <ul>
        <li>
            <label>Idea name</label>
            <input type="text" name="idea_name" required />
        </li>

        <li class="full">
            <label>Description</label>
            <input type="text" name="idea_description" />
        </li>
    </ul>
</form>

这是我的函数(在functions.php中):

add_action( 'wp_ajax_add_new_idea', 'add_new_idea' );
add_action( 'wp_ajax_nopriv_add_new_idea', 'add_new_idea' );

    function ajax_scripts() {
    $parameters = array(
        'ajaxurl' => admin_url('admin-ajax.php'),
        'nonce' => wp_create_nonce('inputs')
    );
    wp_enqueue_script('my-ajax', get_template_directory_uri().'/js/ajax.js', array('jquery'), null, true);
    wp_localize_script('my-ajax', 'inputs', $parameters );
}
add_action('wp_enqueue_scripts', 'ajax_scripts');

function ajaxStatus($status, $message, $data = NULL) {
    $response = array (
        'status'    => $status,
        'message'   => $message,
        'data'      => $data
        );
    $output = json_encode($response);
    exit($output);
}

// New Idea
function add_new_idea() {

    if(isset($_POST["new_idea_form"])) {

        ajaxStatus('error', 'No need to update anything.');

    } else {

        $nonce = $_POST['nonce'];

        if(wp_verify_nonce($nonce, 'inputs') !== false) {

            require_once(ABSPATH . 'wp-admin/includes/taxonomy.php');

            $idea_name      = $_POST['idea_name'];
            $idea_description   = $_POST['idea_description'];
            $idea_slug          = sanitize_title_with_dashes($idea_name);

            $idea = array(
                'cat_name'              => $idea_name, 
                'category_parent'       => '',
                'category_nicename'     => $idea_slug,
                'category_description'  => $idea_description,
                'taxonomy'              => 'ideas'
            );

            wp_insert_category( $idea );

            //print_r($idea);
            //die;

            // Success message
            ajaxStatus('success', 'Added new idea');

        } else {

            // No nonce!
            ajaxStatus('error', 'Nonce failed!');

        }
    }
}

...这是我的 ajax.js:

$('#new_idea').on('submit', function(e) {
    e.preventDefault();

    $.post( inputs.ajaxurl, {
        action : 'add_new_idea',
        nonce : inputs.nonce,
        post : $(this).serialize()
    },
    function(response) {
        console.log(response);
        ResponseSuccess(response);
    });

    return false;

});

至于故障排除,如果我像这样将值硬编码到 $idea 数组中并提交表单...

    $idea = array(
    'cat_name'              => 'cool idea', 
    'category_parent'       => '',
    'category_nicename'     => 'cool-dea',
    'category_description'  => 'a description of my cool idea',
    'taxonomy'              => 'ideas'
);

...它确实有效,我的类别被创建。

所以据我所知,真正的问题是它没有获得提交的 $_POST[] 值,尽管我不明白为什么。

任何帮助都会很棒。

【问题讨论】:

    标签: ajax wordpress frontend categories


    【解决方案1】:

    试试这个代码。

        function add_new_idea() {
    
            $params = array();
            parse_str($_POST["post"], $params);
    
            if(isset($_POST["post"])) {
    
                ajaxStatus('error', 'No need to update anything.');
    
            } else {
    
                $nonce = $_POST['nonce'];
    
                if(wp_verify_nonce($nonce, 'inputs') !== false) {
    
                    require_once(ABSPATH . 'wp-admin/includes/taxonomy.php');
    
                    $idea_name      = $params['idea_name'];
                    $idea_description   = $params['idea_description'];
                    $idea_slug          = sanitize_title_with_dashes($idea_name);
    
                    $idea = array(
                        'cat_name'              => $idea_name, 
                        'category_parent'       => '',
                        'category_nicename'     => $idea_slug,
                        'category_description'  => $idea_description,
                        'taxonomy'              => 'ideas'
                    );
    
                    wp_insert_category( $idea );
    
                    //print_r($idea);
                    //die;
    
                    // Success message
                    ajaxStatus('success', 'Added new idea');
    
                } else {
    
                    // No nonce!
                    ajaxStatus('error', 'Nonce failed!');
    
                }
            }
        }
    

    阅读本文 serlize 对象需要使用parse_str

    Retrieving serialize data in a PHP file called using AJAX

    【讨论】:

      猜你喜欢
      • 2011-12-19
      • 1970-01-01
      • 1970-01-01
      • 2014-11-09
      • 2023-03-11
      • 1970-01-01
      • 2021-07-04
      • 2019-07-04
      • 1970-01-01
      相关资源
      最近更新 更多