【问题标题】:How add_option for array in Wordpress plugin? (e.g. add "file" to list of "files")如何在 Wordpress 插件中为数组添加选项? (例如,将“文件”添加到“文件”列表中)
【发布时间】:2014-03-15 14:09:32
【问题描述】:

我希望我的插件设置页面包含所有当前管理的文件的列表,并允许它们添加新的文件路径。像这样:

浏览器中的虚构布局

Files Managed:
    * /wp-content/plugins/myplugin/files/file1
    * /wp-content/plugins/myplugin/files/file1
    * /wp-content/plugins/myplugin/files/file1
Add New File:
    [File_Name]
    [submit]

我将如何实现这一目标?以前,我只是将页面提交给options.php(Wordpress 内置选项处理程序),但这完全替换了该选项,并且不允许添加另一个选项。

我能够想出的唯一“解决方案”(这似乎很老套)就是自己处理并做到这一点:

/* They've submitted the new file to manage, so we're in a POST situation */

//Get the existing options
$existing = get_option( "managed_files" );

//First time (doesn't exist yet)
if ( $existing === false ) add_option( "managed_files", array( $_POST[ "filename" ] ) );

//Already exists
else
{
    //Add to current values
    array_push( $existing, $_POST[ "filename" ] );

    //Update Option
    update_option( "managed_files", $existing );
}

这是唯一的方法吗?

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    试试这个,

    $existing = get_option( "managed_files" );
    
    //First time (doesn't exist yet)
    if ( empty($existing) ){
        add_option( "managed_files", array( $_POST[ "filename" ] ) );
    }
    //Already exists
    else
    {
        //Add to current values
        //array_push( $existing, $_POST[ "filename" ] );
        $existing = array_merge($existing, array($_POST[ "filename" ]));
    
        //Update Option
        update_option( "managed_files", $existing );
    }
    

    我不确定array_push,但array_merge 可以完美运行。

    希望你能从中得到一点帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-11
      • 2020-01-14
      • 1970-01-01
      • 2022-01-16
      • 2015-04-30
      相关资源
      最近更新 更多