【问题标题】:Wordpress : function create new post after creating a new userWordpress:创建新用户后创建新帖子的功能
【发布时间】:2014-02-06 09:23:28
【问题描述】:

我需要在创建新用户(仅限编辑器)之后创建一个新帖子(自定义帖子类型)。 我想我必须使用这样的钩子:

do_action('user_register', $user_id);
add_action ('user_register', "create_post");
function create_post()
{
// Create the post
}

但我不知道如何在此功能中创建帖子。 而且我在新用户表单中有一些自定义字段...

谢谢 纪尧姆

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    尝试在 add_action 中使用参数,这里是一个简单的例子

    add_action( 'user_register', 'create_post', 10, 1 );
    
    function create_post( $user_id ) {
    
        if ( isset( $_POST['first_name'] ) )
            ..... your code  here
    
    }
    

    对于惰性帖子,请在给定链接http://codex.wordpress.org/Function_Reference/wp_insert_post中使用此功能

    【讨论】:

      【解决方案2】:

      来自wordpress codex user_register

      你可以这样写代码

      add_action( 'user_register', 'myplugin_registration_save', 10, 1 );
      
      function myplugin_registration_save( $user_id ) {
          //Here you can update user information after they registered
          if ( isset( $_POST['first_name'] ) ) {
              update_user_meta($user_id, 'first_name', $_POST['first_name']);
          }
      
      }
      

      要在用户注册后插入帖子尝试扩展代码:

      add_action( 'user_register', 'myplugin_registration_save', 10, 1 );
      
      function myplugin_registration_save( $user_id ) {
          //Here you can update user information after they registered
          if ( isset( $_POST['first_name'] ) ) {
              update_user_meta($user_id, 'first_name', $_POST['first_name']);
          }
      
          // Here you can insert new post for registered users
          $my_post = array(
              'post_title' => 'Title',
              'post_content' => 'some content',
              'post_status' => 'publish',
              'post_author' => $user_id, //If to assign post to the currently registered user otherwise put 1
              'post_type' => 'post'
          );
      
          // Insert the post into the database
          $_post_id = wp_insert_post($my_post);
          if ($_post_id) {
              return 'post inserted';
          }
      }
      

      update_user_meta 可让您在用户注册后更新其他信息
      wp_insert_post 可让您以编程方式插入帖子

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多