Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Export Post Data into json file and Import Post into other website.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.41k
    Comment on it

    Hello readers, In this tutorial I will guide you to create Wordpress Plugin for "Get all Category in Dropdown".

    For creating Wordpress Plugin, we need to do some basic things for creating a Plugin.

    1) Create Wordpress Plugin, just think what your Plugin will do.


    2) Choose a unique name for your Plugin. If you confuse to choose the Plugin name, go to Google and search is Plugin exists or not. In case a Plugin already exists, change your Plugin name.


    3) Create a file inside the Plugin folder and the name of your file is derived from your Plugin name.
        Example:-
            3.1) Plugin Name:- Export-post-data
            3.1) File Name:- export_post_data.php


    4) You can put all the code into one file or you can split your code into multiple files. Your Plugin must have one PHP file.


    5) Your Plugin also contains CSS, js, and images files.


    6) Also, your Plugin must contain One readme.txt file. This readme.txt file contains information about your plugin, like Plugin Name, Plugin URI, Description,
    Author Name, Version, Author URI.

     

    In now days I am working on a project and I have to export all post and import into other website. So I can create a Plugin to do this. If I do this work manually, it take lot of time but with the help of plugin I can do the whole process with in 5 minutes.

     

    In mine code first, we can create a file and put Plugin basic information in comments, which we show below code.

     

    <?php
    /*
    Plugin Name: Export Post Data
    Plugin URI: evontech.com
    Description: Exporting Post Data
    Author: Nitish Rawat
    Version: 1.0
    Author URI: evontech.com
    */
    

     

    Add, CSS and configfile into plugin.

     

    require_once( ABSPATH . '/wp-config.php' );
    
    /****** include css ********/
    add_action( 'admin_print_styles', 'export_admin_styles' );
      
    function export_admin_styles() {
    
        wp_enqueue_style( 'demo-style', plugin_dir_url( __FILE__ ) . '/css/export.css' );    
    }
    /****** include css ********/

     

    Now, Add Custom Menu  on admin panel

     

    /**
     * custom list for display post data
     * create menu in admin dashboard using 'admin_menu hook'
     */
    add_action('admin_menu', 'register_export_post_data_page');
    function register_export_post_data_page() {
    
        add_menu_page( 'Export', 'Export Post Data', 'administrator', 'export_post_data', 'export_post_data', 'dashicons-admin-post', 35.8 );
        add_submenu_page('export_post_data', 'Import Post Data', 'Import Post Data', 'administrator', 'import_post_data', 'import_post_data');
    }

     

    Now, export post data into json file.

     

    /*
    * function for export post data into json file.
    */
    function export_post_data() {
    
        $data = array();
        $args = array(
            'posts_per_page' => -1,
            'post_type'      => 'post',
            'post_status'    => 'publish'
        );
        $query = new WP_Query( $args );
    
        ?>
        <div class="export-post-data">
            <h2>Export Post Data</h2>
            <form name="export_post" id="export_post" action="" method="post">
                <table cellpadding="0" cellspacing="0">
                    <thead>
                        <tr>
                            <th>S No.</th>
                            <th>Post Name</th>
                            <th>Post Content</th>
                            <th>Image</th>
                            <th>category</th>
                        </tr>
                    </thead>
                    <tbody>
    
                <?php
                    $i=1;
                    if($query->have_posts()){
                        while($query->have_posts()){
                            $query->the_post();
    
                            $data[$i]['title']      = get_the_title();
                            $data[$i]['content']    = get_the_content();
                            $data[$i]['image']      = wp_get_attachment_url(get_post_thumbnail_id($post->ID));
                            $cat_name               = get_the_category();
                            $data[$i]['category']   = $cat_name[0]->name;
                ?>
                            <tr>
                                <td width="10%"><?=$i; ?></td>
                                <td width="20%"><?=$data[$i]['title']; ?></td>
                                <td width="50%"><?=$data[$i]['content']; ?></td>
                                <td width="10%"><img src="<?php if($data[$i]['image']){echo $data[$i]['image'];}else{echo $defalut_img;}?>"></td>
                                <td width="10%"><?=$data[$i]['category'];?></td>
                            </tr>
                <?php
                            $i++;
                        }
                    }
                ?>
                    </tbody>
                    <tfoot>
                        <tr>
                            <th>S No.</th>
                            <th>Post Name</th>
                            <th>Author Name</th>
                            <th>Image</th>
                            <th>category</th>
                        </tr>
                    </tfoot>
                </table>
            </form>
        </div>
        <?php
        $result     = $data;
        $plugin_dir = plugin_dir_path( __FILE__ ); // set plugin path.
        $file_per   = chmod($plugin_dir."results.json", 077); // give permission to file.
        $fp         = fopen($file_per, 'w'); // open file where we write the data
    
        if($result){ // condition for checking file permission
            fwrite($fp, json_encode($result)); // write data into json formate.
            fclose($fp); // colse file.
            //echo "Data write on Json formate";
        }
    }

     

    Last step, import post data from json file and create new post with featured image.

     

    function import_post_data() {
    
        $plugin_dir = plugin_dir_path( __FILE__ ); // set plugin path.
        $json_feed  = ($plugin_dir."results.json");
        $json       = file_get_contents($json_feed); // get all contents from json file.
        $obj        = json_decode($json, true); // decode json data into array form.
    
        $j=0;
        foreach($obj as $unique){
    
            /******** insert post ********/
            $pid = '';
            $title = $unique['title'];
            $content = $unique['content'];
            $image = $unique['image'];
    
            $new_post = array(
                'post_title'    => $title,
                'post_content'  => $content,
                'post_status'   => 'publish',
                'post_type'     => 'post'
            );
            $pid = wp_insert_post($new_post);
    
            /******** end insert post ********/
            
            /******** attached featured image *********/
    
            $upload_dir = wp_upload_dir();
            $image_data = file_get_contents($image);
            $filename = basename($image);
            if(wp_mkdir_p($upload_dir['path']))     $file = $upload_dir['path'] . '/' . $filename;
            else                                    $file = $upload_dir['basedir'] . '/' . $filename;
            file_put_contents($file, $image_data);
    
            $wp_filetype = wp_check_filetype($filename, null ); // check image formate or image extension.
            $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, $file, $pid );
            require_once(ABSPATH . 'wp-admin/includes/image.php');
            $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
            $res1= wp_update_attachment_metadata( $attach_id, $attach_data );
            $res2= set_post_thumbnail( $pid, $attach_id );
    
            /******** end attached featured image *********/
            $j++;
        }
        export_post_data();
    }

     

    That's it ! your Wordpress Plugin is created.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: