Support

Account

Home Forums General Issues Flexible content Inception

Solved

Flexible content Inception

  • Hi folks,

    I’ve a bit of an issue today.
    Since many months, I’m using with great satisfaction and no issue flexible content as shortcodes. Let me explain:

    I’ve the following flexible contents: gallery, carousel, etc. all my own customs.
    When I write an article (which can contain many different flexible contents, I’m using them as shortcodes directly in my article.
    For instance, if the first created flexible content is a carousel, I’ll set it and put between two paragraphs of my article [taz_features id=”1″]. A bit further, I could decide to insert a gallery, so I’ll add a new flexible content “gallery”, set it up, and add [taz_features id=”2″] in my article. And so on, you get it.

    This gives me a great flexibility as such a simple shortcodes can be arranged rapidly in the article while ACF provides me the whole benefit of setting up complex things.

    But today, I needed to add a gallery into my carousel (and so the Inception)
    I’ve added and setup my gallery flexible content, let’s say it’s [taz_features id=”4″], then I’ve added my carousel one.
    Inside the rich text of my carousel slide, I wrote [taz_features id=”4″].
    Inside my article content, I wrote [taz_features id=”5″] (which is the id of the carousel).

    At this point, when viewing the post, I’d have expected my carousel to render, and my gallery to be inside it. But at the moment I saved the article on backend, WordPress gave me an error 500. The post won’t load on frontend.
    I can still re-enter my post list in admin and edit my article (no error here), but until I remove [taz_features id=”4″] from my carousel slide content, it’ll always give me an error 500 and the page won’t work.

    Any idea?

  • A bit of informations:
    I’ve set two error_log on my code. The first one gives me the ID of the shortcode, the second, it’s ‘acf_fc_layout’ name.

    # Without [taz_features id=”4″] inside my carousel slide content: All is fine

    [06-Dec-2017 20:05:12 UTC] 1
    [06-Dec-2017 20:05:12 UTC] before_after
    [06-Dec-2017 20:05:12 UTC] 5
    [06-Dec-2017 20:05:12 UTC] carousel

    # With [taz_features id=”4″] inside my carousel slide content: Infinite loop?
    Note that the second error_log never passes…

    [06-Dec-2017 20:06:09 UTC] 1
    [06-Dec-2017 20:06:09 UTC] 4
    [06-Dec-2017 20:06:09 UTC] 4
    [06-Dec-2017 20:06:09 UTC] 4
    [06-Dec-2017 20:06:09 UTC] 4
    [06-Dec-2017 20:06:09 UTC] 4
    [06-Dec-2017 20:06:09 UTC] 4
    [06-Dec-2017 20:06:09 UTC] 4
    [06-Dec-2017 20:06:09 UTC] 4
    [06-Dec-2017 20:06:09 UTC] 4
    [06-Dec-2017 20:06:09 UTC] 4
    [06-Dec-2017 20:06:09 UTC] 4
    [06-Dec-2017 20:06:09 UTC] 4
    [06-Dec-2017 20:06:09 UTC] 4
    [06-Dec-2017 20:06:09 UTC] 4
    [06-Dec-2017 20:06:09 UTC] 4
    [06-Dec-2017 20:06:09 UTC] 4
    [06-Dec-2017 20:06:09 UTC] 4
    [06-Dec-2017 20:06:09 UTC] 4
    [06-Dec-2017 20:06:09 UTC] 4
    [06-Dec-2017 20:06:09 UTC] 4
    [06-Dec-2017 20:06:09 UTC] 4
    [06-Dec-2017 20:06:09 UTC] 4
    [06-Dec-2017 20:06:09 UTC] 4
    [06-Dec-2017 20:06:09 UTC] 4
    [06-Dec-2017 20:06:09 UTC] 4
    [06-Dec-2017 20:06:09 UTC] 4
    [06-Dec-2017 20:06:09 UTC] 4
    [06-Dec-2017 20:06:09 UTC] 4
    [06-Dec-2017 20:06:09 UTC] 4
    [06-Dec-2017 20:06:09 UTC] 4
    [06-Dec-2017 20:06:09 UTC] 4
    [06-Dec-2017 20:06:09 UTC] 4
    […]

    Here’s the part of the code where I do those error_log

    $shortcodeID	= (int)$a['id'];
    error_log($shortcodeID);
    $rowData	= taz_get_field_data($shortcodeID);
    error_log($rowData['acf_fc_layout']);
  • Update. I know where/when the infinite loop occurs.
    Now, I need to find how to solve it (and I’ve not yet found).

    Here’s an extract of my code:

    function taz_get_field_data($shortcodeID){
    	if (class_exists('acf')){
    		$layouts = get_field('taz_features', false, true);
    		return $layouts[$shortcodeID - 1];
    	} else {
    		error_log("taz_get_field_data(): ACF Plugin is not available.");
    		return null;
    	}
    }
    
    function taz_features($atts, $content = null){
    	$a = shortcode_atts(array(
    		'id' => null
    		), $atts);
    
    	$shortcodeID	= (int)$a['id'];
    	$rowData	= taz_get_field_data($shortcodeID);
    
    	switch ($rowData['acf_fc_layout']){
    		case 'gallery':
    			[…]
    			break;
    		case 'carousel':
    			[…]
    			break;
    		[…]
    	}
    }
    add_shortcode('taz_features', 'taz_features');

    Line 3 of the above code, we have: $layouts = get_field(‘taz_features’, false, true);
    When the last parameter is set to true, looks like the shortcode [taz_features id=4] is executed, thus entering again my shortcode function (taz_features), which will run taz_get_field_data(), which run get_field() again, and so on, thus generating the loop 🙁

    Setting false to the format value could solve my problem, but it would imply massive modifications of the rest of my code. I’d definitely like to avoid this.

  • Line 3 of the above code, is there a way to first do
    $layouts = get_field('taz_features', false, false);
    and then only, ask for a formatting of the wanted layout only:
    –> format_value of $layouts[$shortcodeID - 1]

  • I’ve rewrote my function tax_get_field_data another way:

    function taz_get_field_data($shortcodeID){
    	if (class_exists('acf')){
    		if (have_rows('taz_features')){
    			$counter = 0;
    			while (have_rows('taz_features')){
    				the_row();
    				if ($counter === $shortcodeID){
    					return get_row(true);
    				}
    				$counter++;
    			}
    		}
    	} else {
    		error_log("taz_get_field_data(): ACF Plugin is not available.");
    		return null;
    	}
    }

    Since the_row is not formatted, I was hoping for the best and did my formatting only on the wanted row with get_row(true).
    Sadly, even if I don’t have an infinite loop anymore, the result is messed up.
    get_row(), as get_field() are parsing the included shortcodes [taz_features] shortcode! Why??? Why “format_value” cares of “do_shortcode”.
    I’m missing the ability to format the array, to get [content] key rather than [field_583b166074db8_field_57e024d987a79], but I don’t need the actual value to be parsed. This, I’ll take care later on my code.

    Any idea?

  • Ok, I’ve workaround my issue by creating a second shortcode :/

    function taz_embeddedFeatures_shortcode($atts, $content = null){
    	$a = shortcode_atts(array(
    		'id' => null
    		), $atts);
    
    	return '[taz_features id="' . $a['id'] . '"]';
    }
    add_shortcode('taz_embeddedFeatures', 'taz_embeddedFeatures_shortcode');

    I’ve also reverted back to my original taz_get_field_data function:

    function taz_get_field_data($shortcodeID){
    	if (class_exists('acf')){
    		$layouts = get_field('taz_features', false, true);
    		return $layouts[$shortcodeID - 1];
    	} else {
    		error_log("taz_get_field_data(): ACF Plugin is not available.");
    		return null;
    	}
    }

    So now, When I need for instance to insert a taz_features (like a gallery) into another one (like a carousel), I’m writing [taz_embeddedFeatures id=4] into my carousel slide.

    Thank you “Me” ^^

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Flexible content Inception’ is closed to new replies.