将用户生成的选项保存在wp插件中 [英] Save user generated options in wp-plugin

查看:102
本文介绍了将用户生成的选项保存在wp插件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用简单的用户界面为wordpress创建一个口号插件.它应该随机显示一个用户定义的标语以及说它的名字. 在插件选项中,我希望使用户能够动态添加或删除这些标语. 这是我最后想要的一个快速模型,因此您可以更好地理解它:

I want to create a slogan plugin for wordpress with an easy user interface. it shall randomly display a user-defined slogan along with a name who said it. In the plugin options I want to enable a user to dynamically add or delete those slogans. Here's a quick mock-up of what I want in the end so you can understand it better:

有一个添加标语"按钮,它添加了2个新的输入字段以及删除此标语按钮. 因此,如果只有Name1并按下按钮,则会弹出Name2的那些字段. JS部分对我来说没问题,但是在保存这些值时遇到了问题. 如何在选项中添加新值并保存所有动态生成的选项? 甚至更加困难:如果我单击删除Name2的此口号"并单击保存",它也应该从选项中删除此行,并使用当前值更新Name1和Name 3字段.

There's the "Add Slogan" button which adds 2 new input fields along with the delete this slogan button. So if there was only Name1 and you press the button, those fields for Name2 would pop up. The JS part is no problem for me but I have problems saving those values. How can I add new values into options and save all dynamically generated options? And even more difficult: If I'd click Delete this slogan for Name2 and click save, it should remove this line from the options as well and update the Name1 and Name 3 fields with the current values.

这怎么办,我现在有点头绪,而研究插件法典并没有真正的帮助

How can this be done, I'm a bit clueless right now and looking into the Plugin Codex didn't really help

推荐答案

您的意思是这样的吗?

add_action( 'add_meta_boxes', 'dynamic_add_custom_box' );

/* Do something with the data entered */
add_action( 'save_post', 'dynamic_save_postdata' );

/* Adds a box to the main column on the Post and Page edit screens */
function dynamic_add_custom_box() {
    add_meta_box(
        'dynamic_sectionid',
        __( 'My Slogans', 'myplugin_textdomain' ),
        'dynamic_inner_custom_box',
        'post');
}

/* Render the box content */
function dynamic_inner_custom_box() {
    global $post;
    // nonce for verification
    wp_nonce_field( plugin_basename( __FILE__ ), 'dynamicMeta_noncename' );
    ?>
    <div id="meta_inner">
    <?php

    //GEt the array of saved meta
    $slogans = get_post_meta($post->ID,'slogans',true);

    $c = 0;
    //if ( count( $slogans ) > 0 ) {
    if( is_array ( $slogans ) ){
        foreach( $slogans as $slogan ) {
            if ( isset( $slogan['name'] ) || isset( $slogan['slogan'] ) ) {
                printf( '<p>Slogan Name <input type="text" name="slogans[%1$s][name]" value="%2$s" /> -- Slogan Content : <input type="text" name="slogans[%1$s][slogan]" value="%3$s" /><input class="button tagadd remove" type="button" value="%4$s"></p>', $c, $slogan['name'], $slogan['slogan'], __( 'Remove Slogan' ) );
                $c = $c +1;
            }
        }
    }

    ?>
<span id="here"></span>
<input class="button tagadd add" type="button" value="<?php _e('Add Slogan'); ?>">
<script>
    var $ =jQuery.noConflict();
    $(document).ready(function() {
        var count = <?php echo $c; ?>;
        $(".add").click(function() {
            count = count + 1;

            $('#here').append('<p> Slogan Name <input type="text" name="slogans['+count+'][name]" value="" /> -- Slogan Content : <input type="text" name="slogans['+count+'][slogan]" value="" /><input class="button tagadd remove" type="button" value="<?php _e('Remove Slogan'); ?>">' );
            return false;
        });
        $(".remove").live('click', function() {
            $(this).parent().remove();
        });
    });
    </script>
</div><?php

}

/*  saves our custom data when the post is saved */
function dynamic_save_postdata( $post_id ) {
    // verify if this is an auto save routine. 
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
        return;

    // verify Nonce and that the request is valid,
    if ( !isset( $_POST['dynamicMeta_noncename'] ) )
        return;

    if ( !wp_verify_nonce( $_POST['dynamicMeta_noncename'], plugin_basename( __FILE__ ) ) )
        return;

    // GOOD; we are set, find a save data

    $slogans = $_POST['slogans'];

    update_post_meta($post_id,'slogans',$slogans);
}

这篇关于将用户生成的选项保存在wp插件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆