使用WordPress中的Ajax在前端创建类别

问题描述 投票:0回答:1

我很难通过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个回答
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!');

            }
        }
    }

阅读本文需要使用parse_str进行序列化对象。

Retrieving serialize data in a PHP file called using AJAX

© www.soinside.com 2019 - 2024. All rights reserved.