• Resolved tyealia

    (@tyealia)


    Hello I have tried dubugging and it seems that conditional smart logic does not work with PHP, I have got it working perfectly with text snippets trying every logic featured, but whenever I use logic for PHP snippets it only works with the first two options, logged in and logged out, trying any other option for instance only showing on pages doesn’t work.

Viewing 7 replies - 1 through 7 (of 7 total)
  • I am also having the same problem.
    The conditional logic of text snippets works perfectly, but the conditional logic of php snippets doesn’t.

    Plugin Author Mircea Sandu

    (@gripgrip)

    Hi,

    When using conditional logic with PHP snippets, the conditional logic may not work depending on what code your snippet has or the location used for the snippet.

    I made a note to look into limiting certain combinations of settings that we know won’t work but in the end it also depends a lot on the code.

    You can have a PHP snippet that adds a WordPress filter, for example, and loading that in the footer will not have any effect on the code loaded before the footer.

    If you can share more details on the type of code you are trying to use and in which location we can look into what options we have to make that work.

    Thanks,
    Mircea

    Thread Starter tyealia

    (@tyealia)

    Sure, so out of 5 PHP snippets I have going, I tried testing them all, and none of them worked with conditional logic for PHP.

    I will give one of the more important ones I want working as an example.

    This is a snippet that adds meta like category next to the author’s name on the generate press theme.

    I would love to use the functionality to disable this on any post with the “legal pages” tag or category on my site. Unfortunately, with this snippet, no conditional formatting works except for the “logged in” option.

    add_filter( 'generate_header_entry_meta_items', function( $items ) {
        if ( is_single() || is_page() && !is_page( array( 506337)) ) {
            $items = array(
    	    'date',
    	    'author',
            'categories',
    	    'tags'
            );
        } return $items;
    } );
    
    add_action( 'generate_after_entry_header', 'tu_page_meta' );
    function tu_page_meta() {
        if ( is_singular( 'page' )  && !is_page( array( 506337 )) ) : ?>
            <div class="entry-meta">
                <?php generate_posted_on(); ?>
            </div><!-- .entry-meta -->
        <?php endif;
    }
    Thread Starter tyealia

    (@tyealia)

    Here’s one more example that may be easier.

    A simple snippet to change the generate press footer text to say my site’s name. Again no conditional formatting except logged in/logged out works for this one.

    add_filter( 'generate_copyright','tu_custom_copyright' );
    function tu_custom_copyright() {
        ?>
    ? 2022 mysite.com    <?php
    }
    Plugin Author Mircea Sandu

    (@gripgrip)

    Hi @tyealia,

    Thank you for the examples you sent. The issue here is related to the location where you run your snippets. If you set the snippet to “Run Everywhere” – that is too early for most of the conditional checks (except for Logged In/Out as you noticed).
    If you wish to use conditional logic related to pages/post types I recommend using the Site Wide Header location as this runs later, at a point where most information about the page has been loaded so the checks should work.

    One small note regarding the last example, as that’s a filter you should return the value not output it directly, something like this:

    
    add_filter( 'generate_copyright','tu_custom_copyright' );
    function tu_custom_copyright() {
        return '? 2022 mysite.com';
    }
    

    Let me know if that helps.

    Thread Starter tyealia

    (@tyealia)

    This worked perfectly, thank you so much for this and the return tip!

    All 5 of my snippets now work with conditional logic after “Site Wide Header” is selected. May I suggest when PHP snippet is selected and people click conditional logic, perhaps a notice that says Warning: Many PHP snippets only work with location set to site wide header.

    Cheers!

    Hi. The provided explanation doesn’t work for me. I’m trying to load a 3rd party script only on some pages, but the conditional logic is not working.

    add_action( 'wp_enqueue_scripts', 'add_flickity_js' );
    function add_flickity_js() {
    
    	wp_register_script( 'flickity-js', 'https://unpkg.com/flickity@2/dist/flickity.pkgd.min.js', array(), '2.3.0', true );
    	wp_enqueue_script( 'flickity-js' );		
    }

    Code type: PHP script
    Location: Run Everywhere
    Conditions: SHOW the code snippet if PAGE URL Contains fotbol

    This script is working only in Run Everywhere location.
    The conditional logic works only for logged in/logged out

    Am I doing something wrong? Thanks

    • This reply was modified 2 years, 4 months ago by pistolero.
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘PHP Snippets Don’t Work With Conditional Logic’ is closed to new replies.