Category: PHP

Create New page if its not exists in WordPress programmatically

For our customization want to create new page if it’s not exists then place the following snippet in functions.php within your theme folder!.

function create_new_page()
{
    global $user_ID;
    if( get_page_by_title('New Page')==NULL )
    {
	$new_post = array(
		      'post_title' => 'New Page',
		      'post_content' => 'New post content',
		      'post_status' => 'publish',
		      'post_date' => date('Y-m-d H:i:s'),
		      'post_author' => $user_ID,
		      'post_type' => 'page'
		);
	$post_id = wp_insert_post($new_post);
   }
}
add_action('init','create_new_page');

Have any doubt, then comment here!

Increase the_excerpt() content length in wordpress

For our customization want to increase the_excerpt() content length then using the excerpt_length filter. by default expert length is 55. for example you want to change length in to 100 then place the following snippet in functions.php within your theme folder!.

function increase_custom_excerpt_length( $length ) {
    return 100;
}
add_filter( 'excerpt_length', 'increase_custom_excerpt_length', 999 );

Have any doubt, then comment here!

Set wp mail from name when sending mail using wp_mail()

For our customization want to set wp mail from name when sending mail using wp_mail() then place the following snippet in functions.php within your theme folder!.

function set_wp_mail_from_name( $original_email_from )
{
	$cw_email_from_name = 'Testing';
	return $cw_email_from_name;
}
add_filter( 'wp_mail_from_name', 'set_wp_mail_from_name' );

Have any doubt, then comment here!

Check user meta data when user login in WordPress

For our customization want to check user meta when user login in wordpress then place the following snippet in functions.php within your theme folder!.

function check_user_meta_data($user) 
{
	$st=get_user_meta( $user->ID, 'user_status', true);
	 if ($st != 'active') {
        $user = new WP_Error( 'denied', __("ERROR: Account Not Active..") );
    }
    return $user;
		
}
add_action('wp_authenticate_user', 'check_user_meta_data', 10, 2);

Have any doubt, then comment here!

Remove text editor for custom post type in WordPress

For our customization want to remove text editor for custom post type then place the following snippet in functions.php within your theme folder!.

function remove_text_editor_custom_post_type() {
    remove_post_type_support( 'post_type_name', 'editor' );
}
add_action('init', 'remove_text_editor_custom_post_type');

Have any doubt, then comment here!