PHP Developer

Thursday, 25 June 2015

Create a Custom “Lost Password” Email in WordPress

Create a Custom “Lost Password” Email in WordPress : 

We’ve all done it. Not being able to remember passwords is one of the most annoying things about having so many website accounts across the internet. Fortunately, browsers nowadays let you save usernames and passwords for most websites so you don’t have to worry about losing login information that much. However, it does still happen. If you allow user registration on your WordPress site, users will inevitably need to reset their password at some point.
WordPress provides a default password-reset process that involves the user entering their username or email into a form. Once the form is submitted, WordPress sends an email to the user with that username/password that contains a link to reset their password. Once reset, the user can log in using their new password. The thing is that the canned email for this purpose is rather plain. The bright side, though, is that, like most things, WordPress gives us a way to customize this email. The code below does the following:
  1. Changes the “From” email address. This would be the email any reply is sent to.
  2. Changes the “From” name. This shows the recipient who the email is from.
  3. Changes the email subject.
  4. Changes the content type of the email to HTML so we can use markup within the message. Before, it was a plain-text message.
  5. Creates a custom message to use in the email body. This process involves retrieving the user’s data based on their email/username, assembling a URL with appropriate query arguments so WordPress can do the password reset, and adding a custom message with the link to reset the password. The only part you’ll change is the message at the bottom; almost everything else needs to remain the same to work. Just make sure to include the link that lets the user actually reset the password.

Add this code in functions.php :
  1. <?php
    //* Change "From" email address
    add_filter( 'wp_mail_from', function( $email ) {
        return 'hello@mydomain.com';
    });
    //* Change "From" email name
    add_filter( 'wp_mail_from_name', function( $name ) {
        return 'My Website';
    });
    //* Change Subject
    add_filter( 'retrieve_password_title', function() {
        return 'Password Recovery';
    });
    //* Change email type to HTML
    add_filter( 'wp_mail_content_type', function( $content_type ) {
        return 'text/html';
    });

Wednesday, 24 June 2015

Select Subscriber and Admin Users as author of post in admin panel

 Select Subscriber and Admin Users as author of post in admin panel : 


In this select a subscriber or admin an author of a post in the admin so it displays their name as having written the post.

Add this code in functions.php file, then all users with role subscriber or administrator is show in dropdown in Author section of post

Add this code in functions.php file : 

<?php
add_filter('wp_dropdown_users', 'MySwitchUser');
function MySwitchUser($output)
{
//global $post is available here, hence you can check for the post type here
global $post;
$roles = array('administrator', 'editor', 'author', 'contributor', 'subscriber', 'byt_frontend_contributor', 'customer', 'shop_manager', 'pending');

$output = "<select id=\"post_author_override\" name=\"post_author_override\" class=\"\">";
foreach($roles as $role){
$users = get_users('role='.$role);
foreach($users as $user)
{
global $post;
$sel = ($post->post_author == $user->ID)?"selected='selected'":'';
$output .= '<option value="'.$user->ID.'"'.$sel.'>'.$user->user_login.'</option>';
}
}
//Leave the admin in the list    
    $output .= "</select>";

    return $output;
}
?>

Thursday, 9 April 2015

Create Custom Post with all options like Category, Featured image, Excerpt etc. in wordpress

Create Custom Post with all options like Category, Featured image, Excerpt etc. in WordPress


Copy and Paste this code in function.php file. This function create a post of type "servicerequest".

<?php
/*------------------------ Create Custom Post servicerequest START -------------------------------*/

function service_request() {
    $labels = array(
    'name' => 'Services',
    'singular_name' => 'Services',
    'add_new' => 'Add New Services',
    'add_new_item' => 'Add New Services',
    'edit_item' => 'Edit Services',
    'new_item' => 'New Services',
    'all_items' => 'All Services',
    'view_item' => 'View Services',
    'search_items' => 'Search Services',
    'not_found' => 'No Services Found',
    'not_found_in_trash' => 'No Services found in Trash',
    'parent_item_colon' => '',
    'menu_name' => 'Request Services',
    );
    //register_post_type is use to create new post type.
    //"servicerequest" is a post type

    register_post_type( 'servicerequest', array(
        'labels' => $labels,
        'has_archive' => true,
        'public' => true,
        'supports' => array( 'title', 'editor', 'excerpt', 'custom-fields', 'thumbnail', 'author', 'revisions', 'post-formats' ),
        'exclude_from_search' => false,
        'capability_type' => 'post',
    )
    );  
}
add_action( 'init', 'service_request' );

Wednesday, 8 April 2015

Send Email to multiple users when custom post or post is publish by admin

Send Email to multiple users when custom post or post is publish by admin

Step 1 .  Download PHPMailer from Download and save extract file on path "/wp-content/themes/funding_press" with directory name "PHPMailer".


Step 2 .  Paste this code in function.php file.

Add Meta Box Wordpress Editor for Post or Custom Post in Wordpress

Add Meta Box Wordpress Editor for Post or Custom Post in Wordpress

<?php
/*-------- Add Meta Box Wordpress Editor for Post Code START ----------*/

function myplugin_add_custom_box() {
    $screens = array( 'post' );  // Post Type
    foreach ( $screens as $screen ) {
        add_meta_box(
            'myplugin_sectionid',
            __( 'Product Detail', 'myplugin_textdomain' ),
            'myplugin_inner_custom_box',
            $screen
        );
    }
}

Saturday, 4 April 2015

Create a plugin in wordpress to add Multiple Number of posts, Meta Box And Featured Image from CSV

Create a plugin in wordpress to add Multiple Number of posts, Meta Box And Featured Image from CSV

1.  Create a directory with name “csv-upload”.
2.  Create files in directory  “csv-upload”
          a. csv-upload -> csv-upload.php
          b. csv-upload -> csv_queryupload.php
          c. csv-upload -> ajax-loader.gif
          d. csv-upload -> csv      //This the directory where csv file upload

Tuesday, 31 March 2015

Create Template page to show all posts with pagination

Create Template page to show all posts with pagination


STEP 1.  Upload wordpress simple pagination plugin and activete it (https://wordpress.org/plugins/simple-pagination/)

STEP 2. Copy below code as a template page.

<?php
/*
* Template name: All Posts page
*/

?>

<?php get_header();?>

<div class="pop_cause all_cause">
    <h4>All Posts</h4>

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
    <?php $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query();
    $wp_query->query('posts_per_page=20&post_type=project'.'&paged='.$paged);
    ?>
    <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
        <h5><?php the_title();?></h5>
    <?php endwhile; ?>

    <?php if(function_exists('wp_simple_pagination')) { wp_simple_pagination(); }  ?>  //This function is use to show pagination and is exist in Simple Pagination Plugin
    <?php $wp_query = null; $wp_query = $temp;?>
</div> 

<?php get_footer(); ?>