admin管理员组文章数量:1326069
I have a form where users can save/update their profile. I'm trying that at the same time they can create a custome post type (channels) with some of the info set on the form.
Here the code:
/* 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:
Changed the first code after first comment. Separated in another function to create post after submit form by users. But still not working:
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' );
Why it doesn't create custom post channels?
Appreciate your time
I have a form where users can save/update their profile. I'm trying that at the same time they can create a custome post type (channels) with some of the info set on the form.
Here the code:
/* 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:
Changed the first code after first comment. Separated in another function to create post after submit form by users. But still not working:
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' );
Why it doesn't create custom post channels?
Appreciate your time
Share Improve this question edited Aug 10, 2020 at 11:59 Zaesar asked Aug 10, 2020 at 7:28 ZaesarZaesar 1013 bronze badges 01 Answer
Reset to default 0wp_enqueue_scripts
action is not meant to add PHP code to your page. It is used to add JS scripts.
https://developer.wordpress/reference/hooks/wp_enqueue_scripts/
wp_enqueue_scripts is the proper hook to use when enqueuing scripts and styles that are meant to appear on the front end. Despite the name, it is used for enqueuing both scripts and styles.
If you want to handle your post submission, you can use the init
or admin_init
actions (if you are on the public website or the admin section). You might need to hook wp
if you need to access the global $post
object.
Do you get anything in your error handler? Can you check that you correctly get your posted data with var_dump($_POST);
?
Does your post type channels
is registered somewhere, your current user needs to be allowed to create the post type. You should check the capabilities
that you set when registering your post type. https://developer.wordpress/reference/functions/register_post_type/
本文标签: phpWhy it doesn39t create a custom post type after form submitting
版权声明:本文标题:php - Why it doesn't create a custom post type after form submitting 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742196185a2431124.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论