PHP Developer

Thursday, 25 August 2016

Genarate Shortcode and pass values in variables

Genarate Shortcode and pass values in variables :


<?php
/****** Shortcode to get values of any texonomy START ***********/
function custom_cat_shortcode( $attrs ) {
    $output_list = '';
    $category_attrs = shortcode_atts( array(
        'number_of_column' => '1', //default value
        'texonomy_name' => 'vacancies_states', //default value
    ), $attrs );
   
    $number_of_column = $category_attrs[ 'number_of_column' ];
    $texonomy_name = $category_attrs[ 'texonomy_name' ];   
   
    $output_list .= "<strong>Number of Column = </strong>".$number_of_column;
    $output_list .= "<br>";
    $output_list .= "<strong>Texonomy Name = </strong>".$texonomy_name;

    return $output_list;   
}
add_shortcode( 'custom-cat-list', 'custom_cat_shortcode' );
//[custom-cat-list number_of_column="2" texonomy_name="vacancies_uts"] pass values and use this shortcode
// Output is :
//Number of Column = 2
//Texonomy Name = $texonomy_name

?>

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 );
}
?>