Support » Fixing WordPress » Using shortcodes to add ACF groups as panels to other pages

  • I know that Theme Check plugin doesn’t like shortcodes to be used in themes, however I’d like to use the Advanced Custom Fields plugin and create groups (panels) that can be included in other pages with shortcodes. So here is my current setup and below I will post the code as well.

    1. I create main page called ‘Theme Panels’ where all children would be build with ACF plugin groups.
    2. I create a ACF group for each type of panel; e.g. into, into image left, intro imagr right etc.
    3. Then user go to Add New page, select ‘Theme Panels’ and ‘Intro’ as template for example.
    4. Next the panel slug is added to other pages as shortcode. E.g. you may have [intro], [intro-about], [intro-contact] each relying on the Intro template.

    Is this going to pass WordPress theme validation or should I drop it and try another method? Shortcodes are used only for inclusion of panels (as template parts) and nothing else. Hope that someone from the review team would answer my question. Thank you.

    I have working version and the code below is how I did that.

    This is out of functions.php.

    add_action('init', 'generate_dynamic_shortcodes');
    
    	function generate_dynamic_shortcodes() {
    
    		$parent     = get_page_by_title( 'Theme Panels' );
    		$panels     = get_pages( array( 'child_of' => $parent->ID ) );
    
    		foreach ( $panels as $panel ) {
    
    			add_shortcode( 
    				$panel->post_name, 
    				'load_page_panels' 
    			);
    		}
    	}
    
    	function load_page_panels( $atts, $content = null, $shortcode ) {
    
    		$panel_page 	= get_page_by_path( 'theme-panels/' . $shortcode );
    		$panel_page_var = 'panel_page_id_' . str_replace( '-', '_',  $shortcode );
    
    		$GLOBALS['developry_lite_panel'][$panel_page_var] = absint( $panel_page->ID );
    
    		return get_template_part( str_replace( '.php', '', get_page_template_slug( $panel_page->ID ) ) );
    	}

    And here is the template file as reference (intro.php) :

    if ( ! empty( $GLOBALS['developry_lite_panel'] ) ) {
    
    		$panel_id = end($GLOBALS['developry_lite_panel']);
    
    		the_field( 'heading', $panel_id );
    		the_field( 'image', $panel_id );
    		the_field( 'text', $panel_id );
    		the_field( 'content', $panel_id );
    	}
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Steve Stern (sterndata)

    (@sterndata)

    Forum Moderator & Support Team Volunteer

    Theme validation, and dependency on ACF, only matters if you’re submitting a theme to the repository. If this is a site you’re building for a client, it’s no problem.

    Thread Starter Developry

    (@developry)

    I understand that and plan to use it into my premium theme version but I was wondering if I can update my current WordPress.org theme submission with that feature or this will cause for my theme to be declined a directory inclusion?

    Thank you.

    Moderator Steve Stern (sterndata)

    (@sterndata)

    Forum Moderator & Support Team Volunteer

    You’d have to ask the theme review folks. Ask in the #themereview channel on slack.

    Thread Starter Developry

    (@developry)

    Yep, I’m registered in there, Thanks.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Using shortcodes to add ACF groups as panels to other pages’ is closed to new replies.