为什么在提交表单后不创建自定义帖子类型

时间:2020-08-10 作者:Zaesar

我有一个表单,用户可以在其中保存/更新他们的个人资料。我正在尝试,同时他们可以创建一个自定义帖子类型(频道),其中包含表单上的一些信息集。

代码如下:

/* mY CHANNEL INFO FORM UPDATE */

function form_update() {

    global $post;
    if( $post->ID == 153) {

        /* Get user info. */
        global $current_user, $wp_roles;
        /* Load the registration file. */
        $error = array();    
        /* If profile was saved, update profile. */
        if ( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] ) && $_POST[\'action\'] == \'update-user\' ) {

            /* Update user password. */
            if ( !empty($_POST[\'pass1\'] ) && !empty( $_POST[\'pass2\'] ) ) {
                if ( $_POST[\'pass1\'] == $_POST[\'pass2\'] )
                    wp_update_user( array( \'ID\' => $current_user->ID, \'user_pass\' => esc_attr( $_POST[\'pass1\'] ) ) );
                else
                    $error[] = __(\'The passwords you entered do not match.  Your password was not updated.\', \'profile\');
            }

            /* Update user information. */
            if ( !empty( $_POST[\'url\'] ) )
                wp_update_user( array( \'ID\' => $current_user->ID, \'user_url\' => esc_url( $_POST[\'url\'] ) ) );
            if ( !empty( $_POST[\'email\'] ) ){
                if (!is_email(esc_attr( $_POST[\'email\'] )))
                    $error[] = __(\'The Email you entered is not valid.  please try again.\', \'profile\');
                elseif(email_exists(esc_attr( $_POST[\'email\'] )) != $current_user->id )
                    $error[] = __(\'This email is already used by another user.  try a different one.\', \'profile\');
                else{
                    wp_update_user( array (\'ID\' => $current_user->ID, \'user_email\' => esc_attr( $_POST[\'email\'] )));
                }
            }

            if ( !empty( $_POST[\'first-name\'] ) )
                update_user_meta( $current_user->ID, \'first_name\', esc_attr( $_POST[\'first-name\'] ) );
            if ( !empty( $_POST[\'last-name\'] ) )
                update_user_meta($current_user->ID, \'last_name\', esc_attr( $_POST[\'last-name\'] ) );
            if ( !empty( $_POST[\'description\'] ) )
                update_user_meta( $current_user->ID, \'description\', esc_attr( $_POST[\'description\'] ) );
            if ( !empty( $_POST[\'channel_name\'] ) )
                if(channel_exists( esc_attr( $_POST[\'channel_name\']  )) != $current_user->id )
                    $error[] = __(\'This channel is already used by another user.  try a different one.\', \'profile\');
                else {
                    update_user_meta( $current_user->ID, \'channel_name\', esc_attr( $_POST[\'channel_name\'] ) );   
                }
            if ( !empty( $_POST[\'pic\'] ) )
                update_user_meta( $current_user->ID, \'pic\', esc_attr( $_POST[\'pic\'] ) );    
            if ( !empty( $_POST[\'description_channel\'] ) )
                update_user_meta( $current_user->ID, \'description_channel\', esc_attr( $_POST[\'description_channel\'] ) );
    
            /* Redirect so the page will show updated info.*/
            /*I am not Author of this Code- i dont know why but it worked for me after changing below line to if ( count($error) == 0 ){ */
            if ( !$error ) {         
                wp_redirect( get_permalink() .\'?updated=true\' );         
                exit;     
            }
        }


        if ( ! defined( \'ABSPATH\' ) ) {
            exit; // Exit if accessed directly.
        }
    }
}
add_action( \'wp_enqueue_scripts\', \'form_update\'  );

EDITED:

更改了第一条注释后的第一个代码。在用户提交表单后,在另一个功能中分离以创建帖子。但仍然不起作用:

function custom_post () {

    global $post;
    if( $post->ID == 153) {

        /* Get user info. */
        global $current_user, $wp_roles;
        /* Load the registration file. */
        $error = array();    
        /* If profile was saved, update profile. */
        if ( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] ) && $_POST[\'action\'] == \'update-user\' ) {

            if ( !empty( $_POST[\'channel_name\']) && !empty( $_POST[\'description_channel\']) && !empty( $_POST[\'pic\'] ) )  { 
                // Finally, save/update the channel!
                $user = get_user_by( \'ID\', $user_id );
                $channel_id = get_channel_by_author( $user_id );
                $args     = [
                    \'ID\'           => $channel_id,
                    \'post_title\'   => $_POST[\'channel_name\'],
                    \'post_name\'    => $_POST[\'channel_name\'], // set the slug/permalink
                    \'post_content\' => $_POST[\'description_channel\'],
                    \'post_status\'  => \'publish\',
                    \'post_type\'    => \'channels\',
                ];
                $post_id  = wp_insert_post( $args );

                // check if we were succesful.
                if ( is_wp_error( $post_id ) ) {
                    // it didn\'t work!
                    error_log( $post_id->get_error_message() );
                } else if ( empty( $post_id ) ) {
                    // it didn\'t work!
                    error_log( "post_id is empty/false" );
                }
            }
        }
    }

}        

add_action( \'wp\', \'custom_post\' );
为什么它不创建自定义帖子频道?

感谢您的时间

1 个回复
SO网友:Tim

wp_enqueue_scripts 操作并不是要将PHP代码添加到页面中。它用于添加JS脚本。

https://developer.wordpress.org/reference/hooks/wp_enqueue_scripts/

wp\\u enqueue\\u脚本是将要显示在前端的脚本和样式排队时使用的合适挂钩。尽管有这个名称,它还是用于将脚本和样式排队。

如果要处理提交的帖子,可以使用initadmin_init 操作(如果您在公共网站或管理部分)。你可能需要上钩wp 如果需要访问global $post 对象

您是否在错误处理程序中得到任何信息?您是否可以检查您是否正确地使用var_dump($_POST);?

你的帖子类型channels 如果在某处注册,则需要允许当前用户创建帖子类型。You should check the capabilities that you set when registering your post type. https://developer.wordpress.org/reference/functions/register_post_type/