Sunday, 8 July 2018

PHP Wordpress Plugin For Custom Post

In This Tutorial We will Create a Plugin For Custom Post.

Do Below steps for create plugin.

  • Create a folder "wordpress-plugin" in wp-content folder of your wordpress setup.
  • Creat index.php file in wordpress-plugin folder and keep it blank. Reason behind to keep file blank is if somebody visit this path they see only see blank page.So our they can't see our other file.So it's secure for us.
  • Create other file wpplugin.php in wordpress-plugin folder and put below code in it. Need Some php tag for php code

/*
  Plugin Name: WordPress Custom Plugin
  Description: Plugin for testing purpose
  Version: 1
  Author: BS
  Author URI: http://www.google.com
 */
 
 
 // Product Custom Post Type
function product_init() {
    // set up product labels
    $labels = array(
        'name' => 'Products',
        'singular_name' => 'Product',
        'add_new' => 'Add New Product',
        'add_new_item' => 'Add New Product',
        'edit_item' => 'Edit Product',
        'new_item' => 'New Product',
        'all_items' => 'All Products',
        'view_item' => 'View Product',
        'search_items' => 'Search Products',
        'not_found' =>  'No Products Found',
        'not_found_in_trash' => 'No Products found in Trash',
        'parent_item_colon' => '',
        'menu_name' => 'Products',
    );
   
    // register post type
    $args = array(
        'labels' => $labels,
        'public' => true,
        'has_archive' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'rewrite' => array('slug' => 'product'),
        'query_var' => true,
        'menu_icon' => 'dashicons-randomize',
        'supports' => array(
            'title',
            'editor','thumbnail'
          
        )
    );
    register_post_type( 'product', $args );
               
                                                 register_taxonomy(
                                                                'product_category',
                                                                'product',
                                                                array('hierarchical' => true,
                                                                 'label' => 'Category',
                                                                  'query_var' => true,
                                                                   'rewrite' => array( 'slug' => 'product-category' )));

 

 }
 add_action( 'init', 'product_init' );
 
 /**
 * Register meta box(es).
 */
function wpdocs_register_meta_boxes() {
    add_meta_box( 'meta-box-id', __( 'My Meta Box', 'textdomain' ), 'wpdocs_my_display_callback', 'product' );
}
add_action( 'add_meta_boxes', 'wpdocs_register_meta_boxes' );

/**
 * Meta box display callback.
 *
 * @param WP_Post $post Current post object.
 */
function wpdocs_my_display_callback( $post ) {
    // Display code/markup goes here. Don't forget to include nonces!
                $product_author = get_post_meta( $post->ID, "product_author", true );
                <table class="form-table">
                                <tbody>
                                                <tr>
                                                                <th scope="row"><label for="product_author">Product Meta</label></th>
                                                                <td><input name="product_author" id="product_author" class="regular-text" type="text" value=""></td>
                                                </tr>
                                </tbody>
                </table>
}

function wpdocs_save_meta_box( $post_id ) {
    // Save logic goes here. Don't forget to include nonce checks
                update_post_meta( $post_id, "product_author", $_POST["product_author"] );               
}
add_action( 'save_post', 'wpdocs_save_meta_box' );

Monday, 2 July 2018

PHP Wordpress Custom Form Part 3

In Previous Tutorial We Have Created Template For Custom Form. Now add functionality for Validate data and Add data in database.

First we add functionality for validate data inserted in form .

  • To do this create a new js file custom.js with functionality for validation.
  • Put below code in it.

function form_validation(){
     if( jQuery("#form_title").val() == "" ){
           alert("Enter Form Title");
           jQuery("#form_title").focus();
           return false;
     }
    
     if( jQuery("#form_description").val() == "" ){
           alert("Enter Form Description");
           jQuery("#form_description").focus();
           return false;
     }
    
     if( jQuery("#form_image").val() == "" ){
           alert("Select Form Image");
           jQuery("#form_image").focus();
           return false;
     }
    
     if( jQuery("#form_type").val() == "" ){
           alert("Enter Form Type");
          jQuery("#form_type").focus();
           return false;
     }
    
     if( jQuery("#form_author_name").val() == "" ){
           alert("Enter Form Author");
           jQuery("#form_author_name").focus();
           return false;
     }
}

After creating this file put it in js folder in your theme

You need to include this file to call this function.Put below code in your theme's function.php file

wp_enqueue_script( 'custom-js-script', get_template_directory_uri() . '/js/custom.js', array(), '1.0.0', true );

Add Data to Database

For this add below funtion to function.php file

add_action("init", "form_submit_fn");//Add Function

function form_submit_fn(){
     if ( isset( $_POST['form_submit'] ) || wp_verify_nonce( $_POST['form_submit'], 'form_submit_flag' ) ) {
           $form_title = $_POST["form_title"];
           $form_description = $_POST["form_description"];
           $form_image = $_POST["form_image"];
           $form_type = intval( $_POST["form_type"] );
           $form_author_name = $_POST["form_author_name"];
          
          
           // Create post object
           $my_post = array(
             'post_title'    => $form_title,
             'post_content'  => $form_description,
             'post_status'   => 'publish',
             'post_type'     => 'form'
           );
            
           // Insert the post into the database
           $post_id = wp_insert_post( $my_post );
           wp_set_object_terms( $post_id, $form_type, 'genre' );
          
           require_once(ABSPATH . "wp-admin" . '/includes/image.php');
           require_once(ABSPATH . "wp-admin" . '/includes/file.php');
           require_once(ABSPATH . "wp-admin" . '/includes/media.php');
               
           $attach_id = media_handle_upload( "form_image", $post_id );
           if ($attach_id > 0){
                set_post_thumbnail($post_id, $attach_id);
           }
           update_post_meta( $post_id, "form_author", $form_author_name );
     }
}

Saturday, 30 June 2018

PHP Wordpress Custom Form Part 2


In Previous Tutorial We Have Created Template For Custom Form. Now put this file in your theme folder (wp-content\themes\YOUR-THEME).

Now to use this custom form you need to call this template file in your page.


  • To do this create a new page with custom form in your wordpress. 
  • For this open your wordpress admin panel.

  • Add new page from pages menu. Enter Title for page. Then select Template we have created Custom Form. Then click on publish.

  • Open page link and see the form. (For Exa: http://localhost/wordpress/custom-form/) 







Friday, 29 June 2018

PHP Wordpress Custom Form Part 1

Here We Will Learn About How To Create Custom Form In Wordpress.

For Create Custom Form First We Have To Create Template For That. So Below Is A Code For Create Template For Form.

Create A Php Page Name As page-custom-form.php And Add Below Code In That Page.



<?php
/*
Template Name: Custom Form
*/

get_header(); 

?>
<div class="wrap">
     <div id="primary" class="content-area">
           <main id="main" class="site-main" role="main">
                <form method="post" enctype="multipart/form-data" onsubmit="return form_validation();">
                     <div class="form-group">
                           <label>Title</label>
                           <input type="text" name="form_title" id="form_title">
                     </div>
                     <div class="form-group">
                           <label>Description</label>
                           <input type="text" name="form_description" id=" form_description ">
                     </div>
                     <div class="form-group">
                           <label>Image</label>
                           <input type="file" name="form_image" id="form_image">
                     </div>
                     <div class="form-group">
                           <label>Type</label>
                           <select name="form_type" id="form_type">
                           <option value="">- Select Type -</option>
                          <option value="low">Low</option>
<option value="medium">Medium</option>
<option value="hard">Hard</option>

                           </select>
                     </div>
                     <div class="form-group">
                           <label>Form Author</label>
                           <input type="text" name="form_author_name" id="form_author_name">
                     </div>
                     <div class="form-group">
                           <input type="submit" value="Save">
                           <?php wp_nonce_field( 'form_submit_flag', 'form_submit' ); ?>
                     </div>
                </form>
           </div>
     </div>
</div>

<?php get_footer(); ?>