代码之家  ›  专栏  ›  技术社区  ›  Roy

Wordpress-在插件激活时编辑仪表板页面

  •  0
  • Roy  · 技术社区  · 11 年前

    我在大卫·沃尔什的电脑上找到了一段密码 post 关于在用户联系人表单上添加自定义字段。但这是在function.php中完成的。我希望当我的插件被激活时也能这样做。我发现很难在激活钩子上调用add_filter,并在wordpress文档中找到了另一段代码。我合并了它们并尝试了以下代码,但我的插件安装失败了。我知道出了问题,但找不到原因。

    <?php
    function modify_contact_methods($profile_fields) {
    
    // Add new fields
    $profile_fields['twitter'] = 'Twitter Username';
    $profile_fields['facebook'] = 'Facebook URL';
    $profile_fields['gplus'] = 'Google+ URL';
    
    // Remove old fields
    unset($profile_fields['aim']);
    
    return $profile_fields;
    }
    
    /* Main Plugin File */
    function my_plugin_activate() {
    
        add_filter('user_contactmethods', 'modify_contact_methods');
    }
    register_activation_hook( __FILE__, 'my_plugin_activate' );
    
    function load_plugin() {
    
    if ( is_admin() && add_filter('user_contactmethods' ) == 'modify_contact_methods' ) {
    
        delete_option( 'Activated_Plugin' );
    
        /* do stuff once right after activation */
        // example: add_action( 'init', 'my_init_function' );
    }
    }
    add_action( 'admin_init', 'load_plugin' );
    
    1 回复  |  直到 11 年前
        1
  •  0
  •   Hüseyin BABAL    11 年前

    如果你想让它在你的插件激活后工作,并在激活后永远工作,你可以使用以下方法:;

    <?php
    /*
    Plugin Name: Simple Plugin
    Plugin URI: http://huseyinbabal.net
    Description: Simple plugin
    Version: 0.1
    Author: Hsüseyin BABAL
    Author URI: http://huseyinbabal.net
    */
    function modify_contact_methods($profile_fields) {
    
    // Add new fields
    $profile_fields['twitter'] = 'Twitter Username';
    $profile_fields['facebook'] = 'Facebook URL';
    $profile_fields['gplus'] = 'Google+ URL';
    
    // Remove old fields
    unset($profile_fields['aim']);
    
    return $profile_fields;
    }
    
    add_filter('user_contactmethods', 'modify_contact_methods');
    
    推荐文章