Forum Replies Created

Viewing 11 replies - 1 through 11 (of 11 total)
  • Thread Starter tomarmstrong

    (@tomarmstrong)

    Ok, by the sounds of it I have not installed the plugin correctly. I will try to add the latest version again and let you know if there are any issues.

    Thanks for your help!

    Thread Starter tomarmstrong

    (@tomarmstrong)

    Hello Micheal,

    Unfortunately I do not have a dev site set up as I have continued on my website build using the older version.

    I was at an early stage when I came across the image upload problem.

    I was using the latest version of WordPress, the latest version of the roots/sage starter theme and the latest version of CMB2. I saved out the example metabox page as metaboxes.php after removing un-needed fields and changing the prefix to match the website.

    I was using the example file field type between two standard text field types, nothing complex, it was all on a my first template.

    Hopefully this information can be of some help to you, if you would like any more information please just ask!

    Thread Starter tomarmstrong

    (@tomarmstrong)

    I went back to version 2.2.1 and this resolved the styling issue.

    Also, the file upload was not working on version 2.2.2.1, but this has also resolved since going back to version 2.2.1

    Thread Starter tomarmstrong

    (@tomarmstrong)

    Micheal/Justin,

    Thank you very much for your help on this, after implementing the code supplied…

    function tomarmstrong_get_all_terms() {
    	$args = array(
    		'taxonomy' => 'form_category',
    		'hide_empty' => false
    	);
    
    	$terms = get_terms( $args );
    
    	foreach( $terms as $term ) {
    		$select_items[ $term->slug ] = $term->name;
    	}
    
    	return $select_items;
    }

    and

    $cmb_contact_page->add_field( array(
    		'name'    => __( 'Downloads Categorys', 'cmb2' ),
    		'desc'    => __( 'Select the downloads category.', 'cmb2' ),
    		'id'      => $prefix . 'download_cat',
    		'type'    => 'select',
    		'options' => 'tomarmstrong_get_all_terms',
    	) );

    Everything is now working as expected.

    Outstanding first class support, thanks again for the time spent, speedy replies and of course the amazing plugin!

    Thread Starter tomarmstrong

    (@tomarmstrong)

    Hello Micheal/Justin,

    Thank you for your help and advice with this.

    What may be worth tinkering with, since the taxonomy_* field types already populate for you, would be setting the sanitization_cb parameter so that it does end up saving the chosen slug to post meta, and continue from there. I’m not sure what you mean by this and where to implement this? I’m guessing I need to alter:

    'sanitization_cb' => 'sanitize_text_field'

    but I’m not 100% sure in what way.

    I have tired to use ‘select’ instead of taxonomy_select’ but this is no good as i need the drop down list to be dynamic so for example if the user adds a new category this will appear within the drop down list. I know I’d probably need to write a small function to pull through the categories dynamically then pass it to the add_field method where the option array is but I’ll go on to that if all else fails.

    However, the drop down meta box (taxonomy select) in the page editor does not pull the correct category id from the postmeta table.
    Not quite following here.

    At the moment the front end is working fine, when I select one of the categories within the page editor the categories list appears on the front end.

    If i then change the drop down option within the page editor this will then change the category and list displayed on the front end, that is great but the new selected option does not show on the drop down list, it still appears as the original option before it changes.

    To give you an example, on this page I originally selected the category ‘Short breaks for adults’ I then changed this to ‘Reports’, as you can see from the images below the front end list changed as expected but the drop down box in the back end did not.

    Correct front end list for ‘Reports’ category:
    https://imgur.com/a/p1DJ0

    Incorrect category showing as ‘Short breaks for adults’ this should be showing ‘Reports’:
    https://imgur.com/a/rcYVP

    Here is the list of categories available from the forms custom post type:
    https://imgur.com/a/fgaTD

    Again, thank you for your help and time spent on this issue, I really appreciate it!

    Thread Starter tomarmstrong

    (@tomarmstrong)

    Hello Micheal,

    Since my earlier reply I have had a further look into this issue.

    When sanitize is added it adds the entry to the postmeta table. I have also managed to get the category of downloads to display on the page using:

    $download_category = get_post_meta(get_the_ID(), '_ct_contact_download_cat', true);
    
    //echo $download_category;
    global $wpdb;
                        $sql='select term_id from wp_qzsny57q7q_terms where slug = "'.$download_category.'"';
                        $posts = $wpdb->get_results($sql);
                        //print_r($posts);
    
                        foreach ($posts as $post)
                        {
                            //$post->FIELD1                         
    
                            $category_id = $post->term_id;
                        }
    
    ?>

    However, the drop down meta box (taxonomy select) in the page editor does not pull the correct category id from the postmeta table.

    So for instance, you can select a different category from the drop down and save it to the database but this does not update in the metabox on the edit page.

    I have stripped back the full websites code until I resolve this issue, please see the metabox.php code below:

    <?php
    /**
     * Include and setup custom metaboxes and fields. (make sure you copy this file to outside the CMB2 directory)
     *
     * Be sure to replace all instances of 'yourprefix_' with your project's prefix.
     * https://nacin.com/2010/05/11/in-wordpress-prefix-everything/
     *
     * @category YourThemeOrPlugin
     * @package  Demo_CMB2
     * @license  https://www.opensource.org/licenses/gpl-license.php GPL v2.0 (or later)
     * @link     https://github.com/WebDevStudios/CMB2
     */
    
    /**
     * Get the bootstrap! If using the plugin from www.remarpro.com, REMOVE THIS!
     */
    
    if ( file_exists( dirname( __FILE__ ) . '/cmb2/init.php' ) ) {
        require_once dirname( __FILE__ ) . '/cmb2/init.php';
    } elseif ( file_exists( dirname( __FILE__ ) . '/CMB2/init.php' ) ) {
        require_once dirname( __FILE__ ) . '/CMB2/init.php';
    }
    
    /**
     * Conditionally displays a metabox when used as a callback in the 'show_on_cb' cmb2_box parameter
     *
     * @param  CMB2 object $cmb CMB2 object
     *
     * @return bool             True if metabox should show
     */
    function ct_show_if_front_page( $cmb ) {
        // Don't show this metabox if it's not the front page template
        if ( $cmb->object_id !== get_option( 'page_on_front' ) ) {
            return false;
        }
        return true;
    }
    
    /**
     * Conditionally displays a field when used as a callback in the 'show_on_cb' field parameter
     *
     * @param  CMB2_Field object $field Field object
     *
     * @return bool                     True if metabox should show
     */
    function ct_hide_if_no_cats( $field ) {
        // Don't show this field if not in the cats category
        if ( ! has_tag( 'cats', $field->object_id ) ) {
            return false;
        }
        return true;
    }
    
    /**
     * Manually render a field.
     *
     * @param  array      $field_args Array of field arguments.
     * @param  CMB2_Field $field      The field object
     */
    function ct_render_row_cb( $field_args, $field ) {
        $classes     = $field->row_classes();
        $id          = $field->args( 'id' );
        $label       = $field->args( 'name' );
        $name        = $field->args( '_name' );
        $value       = $field->escaped_value();
        $description = $field->args( 'description' );
        ?>
        <div class="custom-field-row <?php echo $classes; ?>">
            <p><label for="<?php echo $id; ?>"><?php echo $label; ?></label></p>
            <p><input id="<?php echo $id; ?>" type="text" name="<?php echo $name; ?>" value="<?php echo $value; ?>"/></p>
            <p class="description"><?php echo $description; ?></p>
        </div>
        <?php
    }
    
    /**
     * Manually render a field column display.
     *
     * @param  array      $field_args Array of field arguments.
     * @param  CMB2_Field $field      The field object
     */
    function ct_display_text_small_column( $field_args, $field ) {
        ?>
        <div class="custom-column-display <?php echo $field->row_classes(); ?>">
            <p><?php echo $field->escaped_value(); ?></p>
            <p class="description"><?php echo $field->args( 'description' ); ?></p>
        </div>
        <?php
    }
    
    /**
     * Conditionally displays a message if the $post_id is 2
     *
     * @param  array             $field_args Array of field parameters
     * @param  CMB2_Field object $field      Field object
     */
    function ct_before_row_if_2( $field_args, $field ) {
        if ( 2 == $field->object_id ) {
            echo '<p>Testing <b>"before_row"</b> parameter (on $post_id 2)</p>';
        } else {
            echo '<p>Testing <b>"before_row"</b> parameter (<b>NOT</b> on $post_id 2)</p>';
        }
    }
    
    add_action( 'cmb2_admin_init', 'yourprefix_register_demo_metabox' );
    /**
     * Hook in and add a demo metabox. Can only happen on the 'cmb2_admin_init' or 'cmb2_init' hook.
     */
    /***** HOME PAGE START *****/
    add_action( 'cmb2_init', 'ct_register_contact_page_metabox' );
    /**
     * Hook in and add a metabox that only appears on the 'home' page
     */
    function ct_register_contact_page_metabox() {
    
        // Start with an underscore to hide fields from custom fields list
        $prefix = '_ct_contact_';
    
        /**
         * Metabox to be displayed on a single page ID
         */
        $cmb_contact_page = new_cmb2_box( array(
            'id'           => $prefix . 'metabox',
            'title'        => __( 'home Page Metabox', 'cmb2' ),
            'object_types' => array( 'page', ), // Post type
            'context'      => 'normal',
            'priority'     => 'high',
            'show_names'   => true, // Show field names on the left
            'show_on'      => array( 'key' => 'page-template', 'value' => 'template-service-default.php' ),
        ) );
    
        $cmb_contact_page->add_field( array(
            'name'     => __( 'Downloads Categorys', 'cmb2' ),
            'desc'     => __( 'Select the downloads category.', 'cmb2' ),
            'id'       => $prefix . 'download_cat',
            'type'     => 'taxonomy_select',
            'taxonomy' => 'form_category',
            'sanitization_cb' => 'sanitize_text_field',
        ) );

    Here is the nessesary code from the php template:

    <?php 
    
    /*
    Template Name: Single Service Template Default
    */
    
    $download_category = get_post_meta(get_the_ID(), '_ct_contact_download_cat', true);
    
    //echo $download_category;
    global $wpdb;
                        $sql='select term_id from wp_qzsny57q7q_terms where slug = "'.$download_category.'"';
                        $posts = $wpdb->get_results($sql);
                        //print_r($posts);
    
                        foreach ($posts as $post)
                        {
                            //$post->FIELD1                         
    
                            $category_id = $post->term_id;
                        }
    
    ?>
    
    <ul>
    	<?php
    	// the query
    	$args = array(
    		'post_type' => 'forms_pt',
    		'orderby'=>'title',
    		'order'=>'ASC',
    		'posts_per_page' => 7,
    		'tax_query' => array(
    		array(
    			'taxonomy' => 'form_category',
    			'field'    => 'id',
    			'terms'    => $category_id,
    			),
    		),
    	);
    
    	$the_query = new WP_Query( $args ); ?>
    
    		<?php if ( $the_query->have_posts() ) : ?>
    
    		<!-- pagination here -->
    
    		<!-- the loop -->
    		<?php while ( $the_query->have_posts() ) : $the_query->the_post();
    
    	?>
    
    	<li><a href="<?= $file; ?>"><?php the_title(); ?></a></li> 
    
    	<?php endwhile; ?>
    	<!-- end of the loop -->
    
    	<!-- pagination here -->
    
    	<?php wp_reset_postdata(); ?>
    
    	<?php else : ?>
    		<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    	<?php endif; ?>
    
    	<li><a href="/downloads/">View All Forms</a></li>
    </ul>

    Again, many thanks for all of your help and support so far, I feel that we are 90% of the way there now!

    Thread Starter tomarmstrong

    (@tomarmstrong)

    Hello Micheal,

    I added the following line of code to my existing metabox code:

    'sanitization_cb' => 'sanitize_text_field',

    Unfortunately this did not work and in result made things worse. Once this line of code was added the drop down box on the back end page editor was not saving, where as before it was but it doesn’t work on the front end.

    array(
         'name'     => 'Downloads Category',
         'desc'     => 'Select the downloads category.',
         'id'       =>  $prefix . 'download_cat_test',
         'type'     => 'taxonomy_select',
         'taxonomy' => 'form_category',
         'sanitization_cb' => 'sanitize_text_field',
    ),
    Thread Starter tomarmstrong

    (@tomarmstrong)

    Hello Micheal,

    I worded that badly. I meant simply use the UI to save a new value. So you’d just go add or edit a post, use the metabox there, set a value, save, and then go check the database. I have tried this method from your intructions and unfortunately this does not make a change to the database. If I set the value needed in the database for a category the relevant list of downloads will appear on the front end, but when the drop down box is then changed in the back end the list disappears on the front end.

    Is there reason why you’re using such an old version of CMB2? Is it a case of a bundled version with the theme? This website was built in 2014, using the released version of sage/roots starter theme at the time.

    Right now, the biggest question is why the post meta isn’t receiving data when saved, if I’m not mistaken. The second question is if it’d work if on the most recent version of the plugin, as 1.1.3 is from April 2014 I have downloaded the latest CMB2 release, added that to my theme folder.

    I have also added the following code to the metaboxes.php file:

    <?php
    /**
     * Include and setup custom metaboxes and fields. (make sure you copy this file to outside the CMB2 directory)
     *
     * Be sure to replace all instances of 'yourprefix_' with your project's prefix.
     * https://nacin.com/2010/05/11/in-wordpress-prefix-everything/
     *
     * @category YourThemeOrPlugin
     * @package  Demo_CMB2
     * @license  https://www.opensource.org/licenses/gpl-license.php GPL v2.0 (or later)
     * @link     https://github.com/WebDevStudios/CMB2
     */
    /**
     * Get the bootstrap! If using the plugin from www.remarpro.com, REMOVE THIS!
     */
    if ( file_exists( dirname( __FILE__ ) . '/cmb2/init.php' ) ) {
        require_once dirname( __FILE__ ) . '/cmb2/init.php';
    } elseif ( file_exists( dirname( __FILE__ ) . '/CMB2/init.php' ) ) {
        require_once dirname( __FILE__ ) . '/CMB2/init.php';
    }
    /**
     * Conditionally displays a metabox when used as a callback in the 'show_on_cb' cmb2_box parameter
     *
     * @param  CMB2 object $cmb CMB2 object
     *
     * @return bool             True if metabox should show
     */
    function yourprefix_show_if_front_page( $cmb ) {
        // Don't show this metabox if it's not the front page template
        if ( $cmb->object_id !== get_option( 'page_on_front' ) ) {
            return false;
        }
        return true;
    }
    /**
     * Conditionally displays a field when used as a callback in the 'show_on_cb' field parameter
     *
     * @param  CMB2_Field object $field Field object
     *
     * @return bool                     True if metabox should show
     */
    function yourprefix_hide_if_no_cats( $field ) {
        // Don't show this field if not in the cats category
        if ( ! has_tag( 'cats', $field->object_id ) ) {
            return false;
        }
        return true;
    }
    /**
     * Manually render a field.
     *
     * @param  array      $field_args Array of field arguments.
     * @param  CMB2_Field $field      The field object
     */
    function yourprefix_render_row_cb( $field_args, $field ) {
        $classes     = $field->row_classes();
        $id          = $field->args( 'id' );
        $label       = $field->args( 'name' );
        $name        = $field->args( '_name' );
        $value       = $field->escaped_value();
        $description = $field->args( 'description' );
        ?>
        <div class="custom-field-row <?php echo $classes; ?>">
            <p><label for="<?php echo $id; ?>"><?php echo $label; ?></label></p>
            <p><input id="<?php echo $id; ?>" type="text" name="<?php echo $name; ?>" value="<?php echo $value; ?>"/></p>
            <p class="description"><?php echo $description; ?></p>
        </div>
        <?php
    }
    /**
     * Manually render a field column display.
     *
     * @param  array      $field_args Array of field arguments.
     * @param  CMB2_Field $field      The field object
     */
    function yourprefix_display_text_small_column( $field_args, $field ) {
        ?>
        <div class="custom-column-display <?php echo $field->row_classes(); ?>">
            <p><?php echo $field->escaped_value(); ?></p>
            <p class="description"><?php echo $field->args( 'description' ); ?></p>
        </div>
        <?php
    }
    /**
     * Conditionally displays a message if the $post_id is 2
     *
     * @param  array             $field_args Array of field parameters
     * @param  CMB2_Field object $field      Field object
     */
    function yourprefix_before_row_if_2( $field_args, $field ) {
        if ( 2 == $field->object_id ) {
            echo '<p>Testing <b>"before_row"</b> parameter (on $post_id 2)</p>';
        } else {
            echo '<p>Testing <b>"before_row"</b> parameter (<b>NOT</b> on $post_id 2)</p>';
        }
    }
    add_action( 'cmb2_admin_init', 'yourprefix_register_demo_metabox' );
    /**
     * Hook in and add a demo metabox. Can only happen on the 'cmb2_admin_init' or 'cmb2_init' hook.
     */

    But this still does not work.

    Do you have any more ideas/possible solutions?

    Thanks again for the help so far!

    Thread Starter tomarmstrong

    (@tomarmstrong)

    Hello Micheal,

    Many thanks for the fast reply!

    I’d get a new dropdown saved, if possible, via the UI that’s generated, and check the database to see how it’s structured in there. By doing this I would be duplicating the current code to create the new drop down box, this will most likely create the same result as the issue I am currently facing.

    I will give you some more information to help you understand this build as much as you can so that you can hopefully help me resolve this issue.

    The website is built on a sage/roots starter theme, I have created templates for most pages that all have a series of metaboxes on for backend use.

    I have added a number of Custom Post Types onto the website that I have created on https://generatewp.com/post-type/ .

    With the theme, CMB2 (version”: “1.1.3”) and custom post types I am using categories, metaboxes and php loops to show a list of downloads.

    From the front end view, on a specific template the user should see a list of downloads relevant to the page they are viewing, I have set up categories in the backend for the ‘forms’ custom post type (please see attached image link below).

    https://imgur.com/a/aiabl

    To pick up the taxonomy of these categories I have used the ‘taxonomy_select’ metabox type:

    array(
           'name'     => 'Downloads Category',
           'desc'     => 'Select the downloads category.',
           'id'       =>  $prefix . 'download_cat',
           'taxonomy' => 'form_category', //Enter Taxonomy Slug
           'type'     => 'taxonomy_select',
    ),

    And then feed this onto the template using the following PHP:

    $download_category = get_post_meta(get_the_ID(), '_ct_download_cat', true);

    ‘$download_category’ is then entered into a PHP loop to display in a list style on the front end:

    <ul>
    	<?php
    		// the query
    		$args = array(
    		'post_type' => 'forms_pt',
    		'orderby'=>'title',
    		'order'=>'ASC',
    		'posts_per_page' => 7,
    		'tax_query' => array(
    	        array(
    		        'taxonomy' => 'form_category',
    		        'field'    => 'id',
    			'terms'    => $download_category,
    		      ),
    	           ),
                    );
    
    $the_query = new WP_Query( $args ); ?>
    
    <?php if ( $the_query->have_posts() ) : ?>
    
    <!-- pagination here -->
    
    <!-- the loop -->
    <?php while ( $the_query->have_posts() ) : $the_query->the_post();
    
    	$file = get_post_meta(get_the_ID(),'form_download', true);
    
    ?>
    
    	<li><a href="<?= $file; ?>"><?php the_title(); ?></a></li> 
    
    <?php endwhile; ?>
    <!-- end of the loop -->
    
    <!-- pagination here -->
    
    <?php wp_reset_postdata(); ?>
    
    	<?php else : ?>
    	     <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    	<?php endif; ?>
    
    </ul>

    The admin user has the option to select what category of downloads they want to select on the front end by using the metabox drop down in the back end (please see image below).

    https://imgur.com/a/lWAcz

    I hope this can give you more of an understanding with what I am up against here, thanks again for the help so far!

    Thread Starter tomarmstrong

    (@tomarmstrong)

    Hi, many thanks for getting back to me.

    In this case, the user is the admin/editor/etc changing the metabox value in the admin area for the specific page? Yes it’s just a drop down on one of the pages edit view.

    Have you confirmed the updated category selection is reflecting in the WP_Query you’re making at the template level?
    I’ve looked at the value of download category and it is not returning anything/returning an empty value.

    $download_category = get_post_meta(get_the_ID(), '_ct_download_cat', true);

    I had a look a bit deeper and if you manually add a row to the table in wp_postmeta it works perfectly, but once it’s changed (drop down in page edit view) it just wipes out the entry. This makes me wonder if it’s something to do with the save as it’s not updating the record correctly. Also when the record is added manually the drop down doesn’t appear to pick it up.

    Do you have caching going on perhaps that would slow down that display? Nope, no caching, I’ve tried it locally and there is nothing like w3cache or anything installed.

    Many thanks for your help (and the plugin!) I really appreciate it.

    Thread Starter tomarmstrong

    (@tomarmstrong)

    Thank you Steve!

Viewing 11 replies - 1 through 11 (of 11 total)