Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Wordpress: Create custom fields for post type at admin end & fetch them at front end

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 94
    Comment on it

    Create custom fields for post type at admin end and fetch them at front end in Wordpress

    The below code will create custom meta box at post type at admin end. You have to put the following code in your theme's function.php file if you want to customize theme only. If you want to create plugin then can use this snippet at plugin activation hook or its function file.

    <?php 
    
    add_action( 'add_meta_boxes', 'my_custom_select_field' );
    function my_custom_select_field() {
        $my_postTypes = array( 'post', 'my_cpt' );  // post type for which you want to display meta box at admin.
        foreach ( $my_postTypes as $my_postType ) {
            add_meta_box(
                'custom_select_id',            
                'Choose Your Rank',     
                'my_custom_box_content',  // callback from where the content/html willbe loaded
                 $my_postType        
            );
        }
    }
    
    /* The below function will contaion the form data to pass at admin metabox with custom fields having  html like text box ,select box or radio button etc. */
    
    function my_custom_box_content( $post ) {
    ?>
       <label for="custom_selct_box"> Description for this field </label>
        <select name="custom_selct_box" id="custom_selct_box">
            <option value="first">First</option>
            <option value="second">Second</option>
        </select>
    <?php
    }
    
    
    /* The below function will save the values entered through meta box from admin end. */
    
    add_action( 'save_post', 'custom_select_save' );  // thi shook will be called to save the values of meta box to db
    function custom_select_save( $post_id ) {
        if ( array_key_exists('custom_selct_box', $_POST ) ) {
            update_post_meta( $post_id,
               'my_select_box_key',  // value will be store in db with this key which will be used to fetch the record at front end.
                $_POST['custom_selct_box']
            );
        }
    }

     

    The below code will be used to fetch the meta values for particular posts. Put the following code to your theme's template file where you want to use or display the meta values by passing meta key of that box.

    /* Code to Fetch the custom meta box value at front end. */
    
    $result = get_post_meta( $post->ID, 'my_select_box_key', true ); ?>
    <select name="custom_selct_box" id="custom_selct_box">
        <option value="first" <?php if ( 'first' == $result ) echo 'selected'; ?>>First</option>
        <option value="second" <?php if ( 'second' == $result ) echo 'selected'; ?>>Second</option>
    </select>


    '$post->ID' will be static or you can use the above snippet in the loop to fetch all the post's custom box values.

 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: