Hide specific posts/pages/cpts from guest users in WordPress

Hide pages/posts/custom post types from guest users in WordPress. The setting can be done on per page/post basis.
If you wanted to hide just a couple pages from guest users, but want it to be available for members, you can use this plugin.

  1. Download this plugin.
  2. Upload and install it via wp-admin plugin installer. Activate the plugin.
  3. A checkbox will appear on edit page screen now:
    Restrict access custom field
    If you check the checkbox, this page will not be viewable unless user is logged in. Guest users will be redirected to login page when they try to view this page.
  4. By default, this checkbox only appears for ‘pages’ and not for ‘blog posts’ or any other custom post types that your website might have. It is however very easy to add it for other post types as well. For example, if you want it for custom post type called ‘course’, you can put the following code in your theme’s functions.php file:
    add_filter( 'restrict_access_post_types', 'myprefix_restrict_access_post_types' );
    function myprefix_restrict_access_post_types( $post_types ){
    	$post_types[] = 'course';
    	return $post_types;
    }

You can download the plugin from link below:


WordPress multisite forgot password url issue

Forgot password/Lost password link in login form on subsite in a WordPress multisite setup has an issue. The link points to main site and not to the subsite where user is actually trying to login.
E.g:
If the visitor is at http://site1.example.com/wp-login.php, forgot password link here will point to http://example.com/wp-login.php?action=lostpassword. This might not be desired in some situations.

Additionally, the email sent for password reset action, also has login links to main site in network and not the subsite where user was actually trying to login.

Code below fixes these 2 issues.

<?php
/*
Plugin Name: Subsite Lostpassword
Plugin URI: http://webdeveloperswall.com/wordpress/multisite-forgot-password-url-issue/
Description: Updates URLs on susbites and in lost password request emails to point to the subsite where the lost password request started.
Version: 1.0
Author: ckchaudhary
Author URI: http://webdeveloperswall.com/wordpress/multisite-forgot-password-url-issue/
*/

/**
 * Major part of the code is taken from https://gist.github.com/strangerstudios/9487278
 */

/*
	Fixes the URL at the bottom of the login page.
*/
function sslp_lostpassword_url($url, $redirect){	
  if( !is_multisite() )
    return $url;
    
	$args = array( 'action' => 'lostpassword' );
	if ( !empty($redirect) ) {
		$args['redirect_to'] = $redirect;
	}

	$lostpassword_url = add_query_arg( $args, site_url('wp-login.php', 'login') );
	return $lostpassword_url;
}
add_filter("lostpassword_url", "sslp_lostpassword_url", 10, 2);

function login_load_jquery(){
	wp_enqueue_script( 'jquery' );
}
add_action( 'login_enqueue_scripts', 'login_load_jquery', 1 );

function wdw_sslp_lostpassword_form_target_script(){
	if( !is_multisite() )
		return;
	
	?>
	<script type="text/javascript">
		jQuery(document).ready(function($){
			$('#lostpasswordform').attr( 'action', '<?php echo add_query_arg( 'action', 'lostpassword', site_url('wp-login.php', 'login') );?>' );
		});
	</script>
	
	<?php 
}
add_action( 'lostpassword_form', 'wdw_sslp_lostpassword_form_target_script' );
/*
	Fixes URL in email that goes out.
*/
function sslp_retrieve_password_message($message, $key){
  if( !is_multisite() )
    return $message;
    
	if ( empty( $_POST['user_login'] ) ) {
		return $message;	//error probably
	} else if ( strpos( $_POST['user_login'], '@' ) ) {
		$user_data = get_user_by( 'email', trim( $_POST['user_login'] ) );
		if ( empty( $user_data ) )
			return $message;	//another error condition, no user found
	} else {
		$login = trim($_POST['user_login']);
		$user_data = get_user_by('login', $login);
	}
	
	$user_login = $user_data->user_login;
	
	$message = __('Someone requested that the password be reset for the following account:') . "\r\n\r\n";
	$message .= home_url( '/' ) . "\r\n\r\n";
	$message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";
	$message .= __('If this was a mistake, just ignore this email and nothing will happen.') . "\r\n\r\n";
	$message .= __('To reset your password, visit the following address:') . "\r\n\r\n";
	$message .= '<' . site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), 'login') . ">\r\n";
	
	return $message;
}
add_filter("retrieve_password_message", "sslp_retrieve_password_message", 10, 2);

This is in the form of a wordpress plugin. Download the zip file from link below. Upload it via wordpress admin plugin installer and activate it network wide.
You can download the plugin from link below:


Add Metabox in wordpress

There are several plugins available in wordpress which helps to add metabox .  It’s a ridiculous  task for worpress programmer to use them as already we have a simple action.

function adding_custom_boxes( $post_type, $post ) {
    add_meta_box( 
        'my-meta-box',
        __( 'My Meta Box' ),
        'render_my_meta_box',
        'post',     // here you can mention your post_type 
               for multiple post type pass array of them  
        'normal',
        'default'
    );
}

// action to add meta box
add_action('add_meta_boxes','adding_custom_boxes',10,2);

//function to add metabox html
function render_my_meta_box(){

}

Once you have metabox in backend, you will need to save metabox data for further retrieval.
You can save metabox data on post save and post update action.

function my_project_save_metabox_data( $post_id ) {
   /*
    *  here you can write your code to save metabox data
    *  you may get it with $_POST['field_name']
    *  you can save it in anywhere or postmeta.  
    */
}
add_action( 'save_post', 'my_project_save_metabox_data' );

Implode and Explode in PHP

We can store multiple value in single variable in two ways.
Either using Array, or comma separated values.

Comma separated: 
$name = 'Alex,Ana,BOB';
Array:  
$name =  array(
    [0] => 'BOB',
    [1] => 'Alex',
    [2] =>  'Ana'
);

To convert array to comma separated value and reverse
we required two function

implode : 
Syntax:  string implode ( string $glue , array $pieces )

$array = array(
    [0] => 'lastname',
    [1] => 'email',
    [2] => 'phone'
);
$comma_separated = implode(",", $array);

result :   will be "lastname,email,phone".
explode : 
Syntax:  array explode (string $delimiter,string $comma_string)

$input2 = "hello,there";
$result = var_dump( explode( ',', $input2 ) );

result : array( 
    [0] => 'hello',
    [1] => 'there'
);

Thanks ……!