严重受困于一些定制元盒/插件的东西

时间:2012-03-16 作者:Dan

我正在开发一个小插件,其中一部分需要向所有自定义帖子类型添加几个元框。我尝试过以各种方式循环使用自定义端口类型的数组,但我不断遇到错误,我已经调用了add\\u meta\\u box等函数。下面的脚本是我的最新成果,它没有保存最后三个元框(仅第一个)中的值。

我真的想缩小这个尺寸。我正在使用一个类,根据我在网络图茨插件课程上学到的一些东西。它在其他方面还可以,但PHP不是我的强项,所以我被卡住了!是否有人可以帮助我循环或将自定义帖子类型的数组传递给函数add\\u meta\\u box?

这是相关脚本的一部分,对不起,这是一个很大的。。

//meta boxes...
    public function meta_boxes(){
    //load the array into a var
    $all_post_types = $this->post_types;

        $just_trades = array();
        foreach($all_post_types as $trade){
            $just_trades[] = $trade[\'post_type_name\'];  
        }
        //call the action 
        add_action(\'add_meta_boxes\',\'make_meta_box\');
        function make_meta_box(){
        //make array of all meta boxes we want to show..

        add_meta_box(\'landline\',\'Landline Phone Number\',\'landline_function\',$just_trades);//id, UI, the function, what page it shows on
        add_meta_box(\'mobile\',\'Mobile Phone Number\',\'mobile_function\',$just_trades);//id, UI, the function, what page it shows on
        add_meta_box(\'email\',\'Email Address\',\'email_function\',$just_trades);//id, UI, the function, what page it shows on
        add_meta_box(\'website\',\'Website URL\',\'website_function\',$just_trades);//id, UI, the function, what page it shows on

        }
        function landline_function($post){ // function that makes the HTML for the admin UI
        $landline = get_post_meta($post->ID,\'landline\',true);// id is available, the key we want, return a single or array value. Single for this.
        ?>
        <label for="landline">Landline Number: </label>
        <input type="text" class="widefat" name="landline" id="landline" value="<?php echo $landline;?>" />
        <?php   
        }
        add_action(\'save_post\',\'save_landline\');
        function save_landline($id){ // ID is available by default 

            if(isset($_POST[\'landline\'])){  
            update_post_meta($id,\'landline\',strip_tags($_POST[\'landline\']));    
            }

        }
        function mobile_function($post){ // function that makes the HTML for the admin UI
        $mobile = get_post_meta($post->ID,\'mobile\',true);// id is available, the key we want, return a single or array value. Single for this.
        ?>
        <label for="mobile">Mobile Number: </label>
        <input type="text" class="widefat" name="mobile" id="mobile" value="<?php echo $mobile;?>" />
        <?php   
        }
        add_action(\'save_post\',\'save_mobile\');
        function save_mobile($id){ // ID is available by default 

            if(isset($_POST[\'mobile\'])){    
            update_post_meta($id,\'mobile\',strip_tags($_POST[\'mobile\']));    
            }

        }
        function email_function($post){ // function that makes the HTML for the admin UI
        $email = get_post_meta($post->ID,\'email\',true);// id is available, the key we want, return a single or array value. Single for this.
        ?>
        <label for="email">Email: </label>
        <input type="text" class="widefat" name="email" id="email" value="<?php echo $email;?>" />
        <?php   
        }
        add_action(\'save_post\',\'save_email\');
        function save_email($id){ // ID is available by default 

            if(isset($_POST[\'email\'])){ 
            update_post_meta($id,\'email\',strip_tags($_POST[\'email\']));  
            }

        }
        function website_function($post){ // function that makes the HTML for the admin UI
        $website = get_post_meta($post->ID,\'website\',true);// id is available, the key we want, return a single or array value. Single for this.
        ?>
        <label for="website">Website: </label>
        <input type="text" class="widefat" name="website" id="website" value="<?php echo $website;?>" />
        <?php   
        }
        add_action(\'save_post\',\'save_website\');
        function save_website($id){ // ID is available by default 

            if(isset($_POST[\'website\'])){   
            update_post_meta($id,\'website\',strip_tags($_POST[\'website\']));  
            }

        }

    }
。。任何帮助都会很好。丹。

2 个回复
SO网友:Geert

下面是一个简单的示例,它向所有公共帖子类型添加了一个元框:

// All public post types
$post_types = array_merge(
    array(\'page\' => \'page\', \'post\' => \'post\'),
    get_post_types(array(\'_builtin\' => FALSE)),
);

// Add meta box for each post type
foreach ($post_types as $post_type)
{
    add_meta_box(\'id\', \'title\', \'callback\', $post_type);
}
WP法典:get_post_types(), add_meta_box().

SO网友:helgatheviking

检修了一些东西。在save\\u post操作中添加了nonce和一些其他权限检查。将所有元合并到一个数组中,并将所有字段合并到一个元框中。最好的主意?谁知道呢,但这几乎和我目前正在研究的代谢箱一模一样。希望有帮助。

//call the action 
add_action(\'add_meta_boxes\',\'make_meta_box\');

function make_meta_box(){


  // All public post types
  $post_types = array_merge(
      array(\'page\' => \'page\', \'post\' => \'post\'),
      get_post_types(array(\'_builtin\' => FALSE)) );

  // Add meta box for each post type
    foreach ($post_types as $post_type) {

             //make array of all meta boxes we want to show..
          add_meta_box(\'contact\',\'Contact Information\',\'contact_function\',$post_type);//id, UI, the function, what page it shows on
    }
}

function contact_function($post){ // function that makes the HTML for the admin UI

  // first thing we\'ll need is a nonce ?>

  <input type="hidden" name="contact_noncename" value="<?php echo wp_create_nonce( "_contact_meta" );?>" />

  <?php

  $meta = get_post_meta($post->ID,\'_contact_meta\',true);

  // landline phone number
  $landline = isset($meta[\'landline\']) ? $meta[\'landline\'] : "";  ?>
  <label for="landline">Landline Number: </label>
  <input type="text" class="widefat" name="_contact_meta[landline]" id="landline" value="<?php echo $landline;?>" />

  <?php   

  // mobile phone number
  $mobile = isset($meta[\'mobile\']) ? $meta[\'mobile\'] : "";   ?>
  <label for="mobile">Mobile Number: </label>
  <input type="text" class="widefat" name="_contact_meta[mobile]" id="mobile" value="<?php echo $mobile;?>" />

  <?php 

  // email address
  $email = isset($meta[\'email\']) ? $meta[\'email\'] : "";    ?>
  <label for="email">Email: </label>
  <input type="text" class="widefat" name="_contact_meta[email]" id="email" value="<?php echo $email;?>" />

  <?php   

  // website
  $website = isset($meta[\'website\']) ? $meta[\'website\'] : "";  ?>
  <label for="website">Website: </label>
  <input type="text" class="widefat" name="_contact_meta[website]" id="website" value="<?php echo $website;?>" />

  <?php   

}

add_action(\'save_post\',\'save_contact\');

function save_contact($post_id, $post){ // ID is available by default 
    if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
          return $post->ID;

    if ( !wp_verify_nonce( $_POST[\'contact_noncename\'], "_contact_meta" )) 
        return $post_id;

    // Check permissions
    if ( \'page\' == $_POST[\'post_type\'] ) {
      if ( !current_user_can( \'edit_page\', $post_id ) )
        return $post_id;
    } else {
      if ( !current_user_can( \'edit_post\', $post_id ) )
        return $post_id;
    }

    // sanitize some things : could do a foreach if you want to strip_tags from all
    $_POST[\'_contact_meta\'][\'landline\'] = strip_tags($_POST[\'_contact_meta\'][\'landline\']);
    $_POST[\'_contact_meta\'][\'mobile\'] = strip_tags($_POST[\'_contact_meta\'][\'mobile\']);
    $_POST[\'_contact_meta\'][\'email\'] = strip_tags($_POST[\'_contact_meta\'][\'email\']);
    $_POST[\'_contact_meta\'][\'website\'] = strip_tags($_POST[\'_contact_meta\'][\'website\']);

    update_post_meta($post_id, \'_contact_meta\', $_POST[\'_contact_meta\']);

}

结束

相关推荐

将定制的PHP输出挂接到WP:如何做到这一点,parse_REQUEST几乎有效,但并不完全有效

我是wordpress的新手,所以很可能我在这里遗漏了一些非常基本的东西。我正在尝试编写一个插件,它使用自定义PHP代码生成输出。我设法在管理方面实现了这一点,但在用户方面,我却找不到一个正常工作的解决方案。我成功地找到了以下两个挂钩:add_action (\'parse_request\', \'myPluginFunc\'); add_action (\'wp\', \'myPluginFunc\'); 当我调用索引时。php?MyPlugin=myPluginFunc这在调用my