PHP Developer

Showing posts with label WordPress. Show all posts
Showing posts with label WordPress. Show all posts

Thursday, 16 November 2017

Create custom carousel slider with custom post type using Slick slider library

Create custom carousel slider with custom post type using Slick slider library



<!------- Create Custom post Type Crousel Slider Using Slick Slider ---------->
<?php
$team_slider_html = '';

$args = array(
    'post_type' => 'product',
    'post_status' => 'publish',
    'posts_per_page' => 10,
);
$query = new WP_Query( $args );
$team_slider_html .= '<section class="center slider hometeam_slider">';
while ( $query->have_posts() ) : $query->the_post();
    if (has_post_thumbnail( get_the_ID() ) ){
        $image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'single-post-thumbnail' );
        $featured_image = $image[0];
    }else{
        $featured_image = site_url().'/wp-content/uploads/2017/10/report-covers.png';
    }
    $team_slider_html .= '<div class="single_team_slide"><div class="single_slide_inner">';
    $team_slider_html .= '<div class="team_member_image"><img src="'.$featured_image.'"></div>';
    $team_slider_html .= '<div class="team_member_excerpt">'.get_the_excerpt().'</div>';
    $team_slider_html .= '<div class="team_member_name">'.get_the_title().'</div>';
    $team_slider_html .= '<div class="team_member_designation">'.get_post_meta( get_the_ID(), '_position', true ).'</div>';
    $team_slider_html .= '</div></div>';
endwhile;
$team_slider_html .= '</section>';

echo $team_slider_html;
?>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css">
<link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.5.9/slick.min.css'>
<link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.5.9/slick-theme.min.css'>
<script src='https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.5.9/slick.min.js'></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
jQuery(document).on('ready', function() {
    jQuery(".center").slick({
        dots: true,
        infinite: true,
        centerMode: true,
        autoplay: true,
        arrows: true,
        slidesToShow: 3,
        slidesToScroll: 1
    });
});
</script>


WordPress Custom Load More Posts Button Without Plugin

WordPress Custom Load More Posts Without Plugin



STEP 1 : Insert this code in functions.php file

<?php
/************ Pagination Function START *************/
function wp_pagination() {
    global $wp_query;
    $big = 12345678;
    $page_format = paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $wp_query->max_num_pages,
        'type'  => 'array'
    ) );
    if( is_array($page_format) ) {
        $paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var('paged');
        echo '<div class="pagination"><ul>';
        echo '<li><span>'. $paged . ' of ' . $wp_query->max_num_pages .'</span></li>';
        foreach ( $page_format as $page ) {
                echo "<li>$page</li>";
        }
       echo '</ul></div>';
    }
}
/************ Pagination Function END *************/
?>

STEP 2 : Template code to get posts with Load morebutton

<?php
   $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
   query_posts(array(  
       'post_type' => 'product', //Post Type
       'post_status' => 'publish', //Post Status
       'paged' => $paged 
   ));
   ?>
<?php if (have_posts()) : ?>
    <section id="posts">
       <?php while (have_posts()) : the_post(); ?>
       <article class="post"><?php echo get_the_title();?></article><br>
       <?php endwhile; ?>
    </section>
    <?php if ( $wp_query->max_num_pages > 1 ) : ?>
        <nav class="load_more">
           <?php next_posts_link( 'Load More' ); ?>
        </nav>
        <script type="text/javascript">
            jQuery(document).ready(function(){
                jQuery('.load_more a').live('click', function(e){
                    e.preventDefault();
                    var link = jQuery(this).attr('href');
                    jQuery('.load_more').html('<span class="loader">Loading More Posts...</span>');
                    jQuery.get(link, function(data) {
                        var post = jQuery("#posts .post ", data);
                        jQuery('#posts').append(post);
                    });
                    jQuery('.load_more').load(link+' .load_more a');
                });
            });
        </script>
    <?php endif;  ?>
<?php endif;  ?>


Monday, 13 November 2017

How to create a Custom Widget in WordPress

How to create a Custom Widget in WordPress


<?php
//Create custom widget 
class Custom_latest_post_Widget extends WP_Widget {
    function __construct() {
        parent::__construct(
            'custom_latest_post_Widget', // Base ID
            'Latest Post List', // Name
            array('description' => __( 'Displays your latest posts. Outputs the post thumbnail, title and date per listing'))
        );
    }
    function widget($args, $instance) { //output
        extract( $args );
        // these are the widget options
        $title = apply_filters('widget_title', $instance['title']);
        $numberOfListings = $instance['numberOfListings'];
        $post_types = $instance['post_types'];
        $thumbnail_image = $instance['thumbnail_image'];
        $enable_excerpt = $instance['enable_excerpt'];
        $excerpt_length = $instance['excerpt_length'];
        $publish_date = $instance['publish_date'];
        $read_more_link = $instance['read_more_link'];
        $read_more_text = $instance['read_more_text'];

        echo $before_widget;
        // Check if title is set
        if ( $title ) {
            echo $before_title . $title . $after_title;
        }
        $this->getRealtyListings($numberOfListings, $post_types, $thumbnail_image, $enable_excerpt, $excerpt_length, $publish_date, $read_more_link, $read_more_text);
        echo $after_widget;
    }

    function update($new_instance, $old_instance) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        $instance['post_types'] = strip_tags($new_instance['post_types']);
        $instance['numberOfListings'] = strip_tags($new_instance['numberOfListings']);
        $instance['thumbnail_image'] = strip_tags($new_instance['thumbnail_image']);
        $instance['enable_excerpt'] = strip_tags($new_instance['enable_excerpt']);
        $instance['excerpt_length'] = strip_tags($new_instance['excerpt_length']);
        $instance['publish_date'] = strip_tags($new_instance['publish_date']);
        $instance['read_more_link'] = strip_tags($new_instance['read_more_link']);
        $instance['read_more_text'] = strip_tags($new_instance['read_more_text']);
        return $instance;
    }
    
    // widget form creation
    function form($instance) {

// Check values
if( $instance) {
$title = esc_attr($instance['title']);
$numberOfListings = esc_attr($instance['numberOfListings']);
$post_types = esc_attr($instance['post_types']);
$thumbnail_image = esc_attr($instance['thumbnail_image']);
$enable_excerpt = esc_attr($instance['enable_excerpt']);
$excerpt_length = esc_attr($instance['excerpt_length']);
$publish_date = esc_attr($instance['publish_date']);
$read_more_link = esc_attr($instance['read_more_link']);
$read_more_text = esc_attr($instance['read_more_text']);
} else {
$title = '';
$numberOfListings = '';
$post_types = '';
$thumbnail_image = '';
$enable_excerpt = '';
$excerpt_length = '';
$publish_date = '';
$read_more_link = '';
$read_more_text = '';
}
?>
        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title', 'custom_latest_post_Widget'); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
         </p>
         <p>
            <label for="<?php echo $this->get_field_id('post_types'); ?>"><?php _e('Post Type:', 'custom_latest_post_Widget'); ?></label>
            <select id="<?php echo $this->get_field_id('post_types'); ?>"  name="<?php echo $this->get_field_name('post_types'); ?>">
               <?php $post_types_array = array('post', 'news', 'product');
                  foreach($post_types_array as $single_post_types){ ?>
               <option <?php echo $single_post_types == $post_types ? 'selected="selected"' : '';?> value="<?php echo $single_post_types;?>"><?php echo $single_post_types; ?></option>
               <?php } ?>
            </select>
         </p>
         <p>
            <label for="<?php echo $this->get_field_id('numberOfListings'); ?>"><?php _e('Number of Listings:', 'custom_latest_post_Widget'); ?></label>
            <select id="<?php echo $this->get_field_id('numberOfListings'); ?>"  name="<?php echo $this->get_field_name('numberOfListings'); ?>">
               <?php for($x=1;$x<=10;$x++): ?>
               <option <?php echo $x == $numberOfListings ? 'selected="selected"' : '';?> value="<?php echo $x;?>"><?php echo $x; ?></option>
               <?php endfor;?>
            </select>
         </p>
         <p>
            <label for="<?php echo $this->get_field_id('thumbnail_image'); ?>"><?php _e('Show Thumbnail Image : ', 'custom_latest_post_Widget'); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id('thumbnail_image'); ?>" type="checkbox" name="<?php echo $this->get_field_name('thumbnail_image'); ?>" <?php if((!empty($thumbnail_image)) && ($thumbnail_image == 'thumb_image')){echo 'checked';}?> value="thumb_image">
         </p>
         <p>
            <label for="<?php echo $this->get_field_id('enable_excerpt'); ?>"><?php _e('Enable Excerpt : ', 'custom_latest_post_Widget'); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id('enable_excerpt'); ?>" type="checkbox" name="<?php echo $this->get_field_name('enable_excerpt'); ?>" <?php if((!empty($enable_excerpt)) && ($enable_excerpt == 'show_excerpt')){echo 'checked';}?> value="show_excerpt">
         </p>
         <p>
            <label for="<?php echo $this->get_field_id('excerpt_length'); ?>"><?php _e('Excerpt Length:', 'custom_latest_post_Widget'); ?></label>
            <select id="<?php echo $this->get_field_id('excerpt_length'); ?>"  name="<?php echo $this->get_field_name('excerpt_length'); ?>">
               <?php for($x=10;$x<=150;$x=$x+10): ?>
               <option <?php echo $x == $excerpt_length ? 'selected="selected"' : '';?> value="<?php echo $x;?>"><?php echo $x; ?></option>
               <?php endfor;?>
            </select>
         </p>
         <p>
            <label for="<?php echo $this->get_field_id('publish_date'); ?>"><?php _e('Publish Date : ', 'custom_latest_post_Widget'); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id('publish_date'); ?>" type="checkbox" name="<?php echo $this->get_field_name('publish_date'); ?>" <?php if((!empty($publish_date)) && ($publish_date == 'date')){echo 'checked';}?> value="date">
         </p>
         <p>
            <label for="<?php echo $this->get_field_id('read_more_link'); ?>"><?php _e('Read More Link : ', 'custom_latest_post_Widget'); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id('read_more_link'); ?>" type="checkbox" name="<?php echo $this->get_field_name('read_more_link'); ?>" <?php if((!empty($read_more_link)) && ($read_more_link == 'more_link')){echo 'checked';}?> value="more_link">
         </p>
         <p>
            <label for="<?php echo $this->get_field_id('read_more_text'); ?>"><?php _e('Read More Text : ', 'custom_latest_post_Widget'); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id('read_more_text'); ?>" name="<?php echo $this->get_field_name('read_more_text'); ?>" type="text" value="<?php echo $read_more_text; ?>" placeholder="Read More" />
         </p>
<?php
    }

    function getRealtyListings($numberOfListings, $post_types, $thumbnail_image, $enable_excerpt, $excerpt_length, $publish_date, $read_more_link, $read_more_text) { //html
        global $post;
        add_image_size( 'custom_latest_post_Widget_size', 85, 45, false );
        /*
        if($post_types == 'product'){
            $tax_query = array(array('taxonomy' => 'products_type','field' => 'slug','terms' => 'market-reports'));
        }else{
            $tax_query = '';
        }
        */

        $args = array(
            'post_type' => $post_types,
            'posts_per_page' => $numberOfListings,
            //'tax_query' => $tax_query
        );

        $listings = new WP_Query($args);

        //$listings->query('post_type='.$post_types.'&posts_per_page=' . $numberOfListings . $new_var );
        if($listings->found_posts > 0) {
            echo '<ul class="custom_latest_post_Widget">';

            while ($listings->have_posts()) {
                $listings->the_post();
                $image = (has_post_thumbnail($post->ID)) ? get_the_post_thumbnail($post->ID, 'realty_widget_size') : '<div class="noThumb"></div>';
                $listItem = '<li>';
                if(!empty($thumbnail_image)){
                    $listItem .= '<div class="list_left_thumb">';
                    $listItem .= $image;
                    $listItem .= '</div>';
                }
                $listItem .= '<div class="list_right_thumb">';
                $listItem .= '<a href="' . get_permalink() . '">';
                $listItem .= get_the_title() . '</a>';
                if(!empty($enable_excerpt)){
                    if(!empty($excerpt_length)){
                        $length = $excerpt_length;
                    }else{
                        $length = 100;
                    }
                    $listItem .= '<div class="list_excerpt">';
                    $listItem .= substr(get_the_excerpt(), 0, $length).'...';
                    $listItem .= '</div>';
                }
                if(!empty($publish_date)){
                    $listItem .= '<div class="list_publish_date">'.date('M d Y', strtotime(get_the_date())).'</div>';
                }
                if(!empty($read_more_link)){
                    $listItem .= '<div class="list_link"><a href="'.get_the_permalink().'">';
                    if(!empty($read_more_text)){
                        $listItem .= $read_more_text;
                    }else{
                        $listItem .= 'Read More';
                    }
                    $listItem .= '</a></div>';
                }
                $listItem .= '</div>';
                $listItem .= '</li>'; 
                echo $listItem; 
            }

            echo '</ul>';
            wp_reset_postdata();
        }else{
            echo '<p style="padding:25px;">No listing found</p>';
        } 
    }

} //end class custom_latest_post_Widget
register_widget('Custom_latest_post_Widget');
?>

Thursday, 15 September 2016

Wordpress Custom Category Template WIth Custom Pagination

Wordpress Custom Category Template WIth Custom Pagination


1) Create new file for custom taxonomy and upload in wordpress theme taxonomy-(taxonomy-name).php
    Example : Id Taxonomy name is "vacancies_uts". Then file name is "taxonomy-vacancies_uts.php".


2) Add this code in functions.php file for pagination anywhere in website

 
<?php
/************ Pagination Function START *************/
function wp_pagination() {
    global $wp_query;
    $big = 12345678;
    $page_format = paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $wp_query->max_num_pages,
        'type'  => 'array'
    ) );
    if( is_array($page_format) ) {
        $paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var('paged');
        echo '<div><ul>';
        echo '<li><span>'. $paged . ' of ' . $wp_query->max_num_pages .'</span></li>';
        foreach ( $page_format as $page ) {
                echo "<li>$page</li>";
        }
       echo '</ul></div>';
    }
}
/************ Pagination Function END *************/
?>
   
3) Add below code in "taxonomy-vacancies_uts.php" and upload in activated theme.

<?php
/**
 * The template for displaying all pages.
 *
*/


get_header(); ?>
<?php

$queried_object = get_queried_object();
$term_id = $queried_object->term_id;
$term_name = $queried_object->name;
?>
<div id="content" class="content" role="main">

<?php
$today = date('m/d/Y');
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

query_posts(array(
//'posts_per_page' => 2,
'post_type' => 'vacancies', //Post Type
'post_status' => 'publish', //Post Status
'tax_query' => array(
    array(
        'taxonomy' => 'vacancies_uts', //Texonomy Name
        'field' => 'id',
        'terms' => $term_id
    )
),
'meta_query' => array(
    array(
        'key' => 'last_date',  //Meta Field name for compare
        'value' => $today,     //Meta Field Value compare with this value
        'compare' => '>='
    )
),
'paged' => $paged
)
);
?>
<?php if ( have_posts() ) : ?>
<div class="cat_heading"><div class="cat_name"><?php echo $term_name;?></div></div>
<table class="vacancies_tables">
<?php while ( have_posts() ) : the_post(); ?>
    <?php $vacancies_elgible = get_the_terms( get_the_ID(), 'vacancies_elgible' );?>
    <tr>
        <td class="vac_title" data-label="Exam"><a href="<?php echo get_the_permalink();?>"><?php echo get_the_title();?></a></td>
        <td class="detail_link" data-label="Details"><a class="home_viewmore" href="<?php echo get_the_permalink();?>">View More</a></td>
    </tr>
<?php endwhile; ?>
</table>
<div class="custom_pagination"><?php wp_pagination(); ?></div>
<?php else : ?>

<?php //get_template_part( 'no-results', 'page' ); ?>
<?php echo "Sorry, no vacancies available under this Category."; ?>

<?php endif;?>

</div><!-- #content -->

<?php get_footer(); ?>

Thursday, 25 August 2016

How wp_editor and date picker use in Posts Meta Fields(Custom Fields)

How wp_editor and date picker use in Posts Meta Fields(Custom Fields) :


<?php
/*----------- Create Metabox in Custom Post vacancies Code Start ---------------*/

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

function myplugin_inner_custom_box( $post ) {
  wp_nonce_field( 'myplugin_inner_custom_box', 'myplugin_inner_custom_box_nonce' );
 
  $job_title = get_post_meta( $post->ID, 'job_title', true );
  $opening_date = get_post_meta( $post->ID, 'opening_date', true );
  $last_date = get_post_meta( $post->ID, 'last_date', true );
 
  $pay_scale_desc = get_post_meta( $post->ID, 'pay_scale_desc', false );
 
  ?>
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css">
  <script>
  $( function() {
    $( ".datepicker" ).datepicker();
  } );
  </script>
  <?php
  echo '<table>';
  echo '<tr>';
  echo '<td><label for="myplugin_new_field">';
       _e( "Job Title", 'myplugin_textdomain' );
  echo '</label> </td>';
  echo '<td><input type="text" id="job_title" name="job_title" value="' . esc_attr( $job_title ) . '" size="25" /></td>';
  echo '</tr><tr>';
  echo '<td><label for="myplugin_new_field">';
       _e( "Opening Date", 'myplugin_textdomain' );
  echo '</label> </td>';
  echo '<td><input type="text" class="datepicker" id="opening_date" name="opening_date" value="' . esc_attr( $opening_date ) . '" size="25" /></td>';
  echo '</tr><tr>';
  echo '<td><label for="myplugin_new_field">';
       _e( "Closing Date", 'myplugin_textdomain' );
  echo '</label> </td>';
  echo '<td><input type="text" class="datepicker" id="last_date" name="last_date" value="' . esc_attr( $last_date ) . '" size="25" /></td>';
  echo '</tr>';
  echo '<tr>';
  echo '<td><label for="myplugin_new_field">';
       _e( "Pay Scale Description", 'myplugin_textdomain' );
  echo '</label> </td>';
  echo '<td>';
  wp_editor( $pay_scale_desc[0], 'pay_scale_desc' );
  echo '</td>';
  echo '</tr>';
  echo '</table>';
}
function myplugin_save_postdata( $post_id ) {
  if ( ! isset( $_POST['myplugin_inner_custom_box_nonce'] ) )
    return $post_id;
  $nonce = $_POST['myplugin_inner_custom_box_nonce'];
  if ( ! wp_verify_nonce( $nonce, 'myplugin_inner_custom_box' ) )
      return $post_id;
  if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
      return $post_id;
  if ( 'page' == $_POST['post_type'] ) {
    if ( ! current_user_can( 'edit_page', $post_id ) )
        return $post_id;
  } else {
    if ( ! current_user_can( 'edit_post', $post_id ) )
        return $post_id;
  }
 
  $job_title = sanitize_text_field( $_POST['job_title'] );
  $opening_date = sanitize_text_field( $_POST['opening_date'] );
  $last_date = sanitize_text_field( $_POST['last_date'] );
 
  update_post_meta( $post_id, 'job_title', $job_title );
  update_post_meta( $post_id, 'opening_date', $opening_date );
  update_post_meta( $post_id, 'last_date', $last_date );
 
  if ( isset ( $_POST['pay_scale_desc'] ) ) {
    update_post_meta( $post_id, 'pay_scale_desc', $_POST['pay_scale_desc'] );
  }
}
add_action( 'save_post', 'myplugin_save_postdata' );

/*----------- Create Metabox in Custom Post vacancies Code END ---------------*/
?>

 =========================================================

And How wp_editor value get in Frontend in same Format :


<?php 
$pay_scale_desc = get_post_meta( $post->ID, 'pay_scale_desc', false );
echo "<pre>";
print_r($pay_scale_desc[0]);
echo "</pre>";
?>

Friday, 19 August 2016

Custom Login Form in Wordpress

Custom Login Form in Wordpress :


<?php

/*
  Plugin Name: Custom Login Form
  Plugin URI:
  Description: Simple Login form plugin that just work, customize easily in code and set redirection or condition according to need.
  Version: 1.0.0
  Author: Sunil Sharma
  Author URI:
 */


function dlf_form() {
?>
<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
    <div class="login-form">
        <h5>Your Username or Email</h5>
        <div class="form-group">
            <input name="login_name" type="text" class="form-control login-field" value="" placeholder="Enter Username or Email" id="login-name" />
            <label class="login-field-icon fui-user" for="login-name"></label>
        </div>
       
        <h5>Your Password</h5>
        <div class="form-group">
            <input  name="login_password" type="password" class="form-control login-field" value="" placeholder="Password" id="login-pass" />
            <label class="login-field-icon fui-lock" for="login-pass"></label>
        </div>
       
        <?php if(isset($_GET["value"]) && $_GET["value"] != "") { ?>
            <input type="hidden" value="<?php echo $_GET["value"];?>" name="service_redirect">
        <?php } ?>
       
        <div class="form-group">
            <input name="rememberme" type="checkbox" id="lwa_rememberme" value="forever" /> <label ><?php _e( 'Remember Me', 'funding' ) ?></label>
        </div>
        <input class="btn btn-primary btn-lg btn-block" type="submit"  name="dlf_submit" value="SIGN IN TO BACKYOURCAUSE" />
    </div>
</form>

<?php
}

function dlf_auth( $username, $password, $service_red ) {
global $user;
$creds = array();

$user = get_user_by('email',$username);
if(!empty($user->user_login)){
    $username = $user->user_login;
}
$creds['user_login'] = $username;
$creds['user_password'] =  $password;
$creds['service_redirect'] =  $service_red;
$creds['user_status'] =  0;
$creds['remember'] = true;
$user = wp_signon( $creds, false );
if ( is_wp_error($user) ) {
echo $user->get_error_message();
}
if ( !is_wp_error($user) ) {
    $user_id = $user->data->ID;
    $user_query = "SELECT `user_status` FROM `wp_users` WHERE `ID` = '".$user_id."'";
    $user_run = mysql_query($user_query);
    $user_value = mysql_fetch_object($user_run);
    $user_status = $user_value->user_status;

    $user_role = $user->roles[0];
   
    if($user_status == 1){
        wp_logout();
        $newURL = site_url()."/sign-in/?msg=Please Confirm your email id";
        header('Location: '.$newURL);
    } else {
        if($creds['service_redirect'] != ""){
            $newURL = $creds['service_redirect'];
            header('Location: '.$newURL);
        } else {
            if($user_role == "administrator"){
                $newURL = site_url()."/wp-admin";
                header('Location: '.$newURL);
            }
            if($user_role == "business"){
                $newURL = site_url()."/business-profile";
                header('Location: '.$newURL);
            }
            if($user_role == "subscriber"){
                $newURL = site_url()."/my-profile";
                header('Location: '.$newURL);
            }
        }
       
    }
}
}

function dlf_process() {
    if (isset($_POST['dlf_submit'])) {
        dlf_auth($_POST['login_name'], $_POST['login_password'], $_POST['service_redirect']);
    }
    dlf_form();
}

function dlf_shortcode() {
    ob_start();
    dlf_process();
    return ob_get_clean();
}

add_shortcode('dm_login_form', 'dlf_shortcode');

//[dm_login_form] use this shortcode to show login form in front end
?>

How Featured Image Assign and upload for Post(By Post ID)

How Featured Image Assign and upload for Post(By Post ID):

<?php
$post_id = $post_id; //post id for which featured image upload
$uploaddir = wp_upload_dir();
$selected_postImage = $_FILES["files"]["name"];
$project_postImage = $uploaddir['path'] . '/' . $selected_postImage;
if ($selected_postImage != '') {
    $filename   = basename($project_postImage);
    $wp_filetype = wp_check_filetype( $project_postImage, null );
    $attachment = array(
         'post_mime_type' => $wp_filetype['type'],
         'post_title'     => sanitize_file_name( $filename ),
         'post_content'   => '',
         'post_status'    => 'inherit'
    );
    $attach_id = wp_insert_attachment( $attachment, $project_postImage, $post_id );
   
    require_once(ABSPATH . 'wp-admin/includes/image.php');
   
    $uploadfile = $uploaddir['path'] . '/' . $filename;
    move_uploaded_file( $_FILES['files']['tmp_name'] , $uploadfile );
    $attach_data = wp_generate_attachment_metadata( $attach_id, $project_postImage );
    wp_update_attachment_metadata( $attach_id, $attach_data );
    set_post_thumbnail( $post_id, $attach_id );
}
?>

Tuesday, 3 May 2016

How to create category from frontend in Wordpress



How to create category from frontend in Wordpress (Also for custom taxonomy)


<?php
$custom_texonmy = 'services_categories';   // Custom texonomy type slug
$job_category = 'Access Covers & Gratings';  //Category name
$cat = get_term_by('name',htmlspecialchars($job_category),'services_categories');  //get category by name
if($cat){
    //This code run id category with above name already exist
    $cat_ID = $cat->term_id; //term_id of category
    $job_type = $cat->slug; //Slug of category
}

if($cat_ID == 0) {
    //This code run id category with above name not exist and create new category
    $cat_name = $job_category;
    $cat_insert = wp_insert_term(
        $cat_name,
        $custom_texonmy
    );
    $cat_ID = $cat_insert['term_id'];
    if($cat_ID != 0){
        $new_cat = get_term_by( 'term_id', $cat_ID, $custom_texonmy );
        if($new_cat){
            $job_type = $new_cat->slug;
        }
    }
} else {
    $cat_ID;
    $job_type = $job_type;
}

?>

How to assign category or “custom taxonomy” category to post from front end



How to assign category or “custom taxonomy” category to post from front end


<?php
$custom_texonmy = 'services_categories';  // Custom texonomy type slug
$job_category = 'Access Covers & Gratings';  //Category name
$post_id = $post_id; //Post id for which assign above category
$cat = get_term_by('name',htmlspecialchars($job_category),$custom_texonmy);  //get category by name
if($cat){
    $cat_ID = $cat->term_id;  // term_id of category
    $job_type = $cat->slug;  //Slug of category
}
if ($job_type) {
    $services_categories = wp_set_object_terms($post_id, $job_type, $custom_texonmy );  //This code assign category of custom texonomy to post from front end
}

?>