[jQuery]Adding shortcode

时间:2015-09-13 作者:Sjors Hijgenaar

我整个晚上都在为此奋斗,没有希望实现我的目标。我已经浏览了好几本书的支持页,但我无法让它发挥作用。

我正在尝试为我的网站实现一个反馈按钮,当点击时,会显示一个忍者表单。由于动态特性,我使用jQuery(我是新手)实现了html,现在我正在尝试将PHP短代码引入其中。

我试过使用AJAX in Plugins, 基本上什么都不做。

feedback\\u按钮。php:

add_action( \'admin_enqueue_scripts\', \'my_enqueue\' );
function my_enqueue($hook) {
    if( \'index.php\' != $hook ) {
        return;
    }

    wp_enqueue_script( \'feedback-script\', plugins_url( \'feedback-script.js\', __FILE__ ), array(\'jquery\') );

    wp_localize_script( \'feedback-script\', \'ajax_object\',
            array( \'ajax_url\' => admin_url( \'admin-ajax.php\' ), \'we_value\' => 1234 ) );
}   

add_action( \'wp_ajax_my_action\', \'my_action_callback\' );
function my_action_callback() {
    global $wpdb;
    $whatever = intval( $_POST[\'whatever\'] );
    $whatever += 10;
    echo $whatever;
    wp_die();
反馈脚本。js公司:

var $buttonDiv = $( "<div id=\'feedback-button\' style=\'display:none;\'>feedback</div>" );
var $formDiv = $( "<div id=\'feedback-form\' style=\'display:none;\'></div>" );

$( document ).ready(function() {
    $( "#content .container" ).append( $buttonDiv );
    $( "#feedback-button" ).css("top", ( $( window ).height() - $( "#feedback-button" ).height() - $( "#main-nav" ).height() ) / 2 + "px" );
    $( "#content .container" ).append( $formDiv );
    $( "#feedback-form" ).css("top", ( $(window ).height() - $( "#feedback-form" ).height() - $( "#main-nav" ).height() ) / 2 + "px" );


    if ( ($(window).width() > 824) && $(window).height() > 289 ) {
        $( "#feedback-button").show(\'slow\');

        $( "#feedback-button" ).click(function() {
            $( "#feedback-button" ).hide(\'slow\');

            var data = {
                \'action\': \'my_action\',
                \'whatever\': ajax_object.we_value      // We pass php values differently!
            };
            // We can also pass the url value separately from ajaxurl for front end AJAX implementations
            jQuery.post(ajax_object.ajax_url, data, function(response) {
                console.log(\'Got this from the server: \' + response);
            });

            $( "#content .container" ).append( $formDiv );
            $( "#feedback-form").show();
        });
    };
});

$( window ).resize(function() {
    if ( ($(this).width() < 825) || $(this).height() < 290 ){
        $( \'#feedback-button\' ).hide();
        $( \'#feedback-form\' ).hide();
    };

    if ( ($(this).width() > 824) && $(this).height() > 289 ){
        $( \'#feedback-button\' ).show( \'slow\' );
    };

    $( "#feedback-button" ).css("top", ( $( window ).height() - $( "#feedback-button" ).height() ) / 2 + "px" );
    $( "#feedback-form" ).css("top", ( $(window ).height() - $( "#feedback-form" ).height() ) / 2 + "px" );
});
我试过使用this page 还有,之后工作#feedback-button 甚至消失了。

(JS脚本相同)feedback\\u按钮。php:

wp_enqueue_style( \'feedback-style\', plugins_url( \'/feedback-style.css\' , __FILE__ ) );

// Register the script
wp_register_script( \'feedback-script\', plugins_url( \'feedback-script.js\' , __FILE__ ) );

// Localize the script with new data
$done_form_shortcode = do_shortcode( \'[ninja_forms id=6]\' );
wp_localize_script( \'feedback_script_handle\', \'feedback_object\', $done_form_shortcode );

// Enqueued script with localized data.
wp_enqueue_script( \'feedback_script_handle\' );
?>
<script>
    $( document ).ready( function() {
        if ($( "#feedback-form" ).length){
            alert( feedback_object.$done_form_shortcode);
        };
    });
</script>
有人能帮我吗?

1 个回复
最合适的回答,由SO网友:Sjors Hijgenaar 整理而成

看到这个表单是为了反馈,不需要太多,我自己实现了一个简单的表单。

jQuery:

<form method=\'post\' id=\'feedback-form\'>\\
    <textarea name=\'feedback-message\' id=\'feedback-message\' rows=\'10\' cols=\'10\' required />\\
    <input type=\'submit\' name=\'feedback-submit\' id=\'feedback-submit\' class=\'ninja-forms-field  btn btn-primary custom-button red-btn\' value=\'Thanks!\' \\
    placeholder=\'Type your comments/suggestions/functionality requests\' />\\
</form>\\
PHP

add_action( \'init\', \'feedback_form_submit\' );

function feedback_form_submit() {
    if(isset($_POST[\'feedback-submit\'])) {
        //here you\'ll have your form data in a $_POST array, you can check it using a print_r. parse the form and insert the post
        $message = $_POST[\'feedback-message\'];
        $user = wp_get_current_user();
        $from_first_name = $user->user_firstname;
        $from_last_name = $user->user_lastname;
        $from_email = $user->user_email;
        $headers = \'From: "\' . $from_first_name . \' \' . $from_last_name . \'" <\' . $from_email . \'>\';



        // mail message to admin
        $to = \'[email protected]\';
        $subject = \'[SRC Thor] New feedback\';
        mail( $to, $subject, $message, $headers);

        wp_redirect(home_url(\'/index.php/nl/thank-you\'));
        exit;

    }
}

相关推荐

自定义模板earch.php中的搜索结果

现在我在搜索结果页面(search.php)中遇到了一个问题,结果是由索引页面模板显示的,而不是它的模板,它显示所有帖子,不管搜索者是否输入了内容我的索引。php页面<?php get_header(); ?> <?php $search = $_get[\'search\']; $args = array( \'post_title_like\' => $search ); $res = new wp_query($args