• Issues with code snippet execute the code later than a theme functions.php which cause invalid functions or functions that isn’t worked as other plugins code execute before code snippet.

Viewing 14 replies - 1 through 14 (of 14 total)
  • Plugin Author ImageShea Bunge

    (@bungeshea)

    Code in snippets executes before functions.php code, but after plugins – seeing as the code is run by a plugin.

    If you want to execute some code after the theme is loaded, you can use the after_setup_theme action:

    add_action( 'after_setup_theme', function () {
        // code here
    } );
    Thread Starter ImageRookie

    (@alriksson)

    Yes but it’s a sql query to db to execute php which happens after. In my case I can not see it works the way you say as other plugins execute before which runs php. And if I want to alter or change a filter or action in another plugin it doesn’t work or execute too late meaning the code and function don’t work as it should.

    Example is once I want to execute code before ACF or ACF extended.

    Maybe I’m wrong but based on this and working testing function.php instead I could just narrow it down to code snippets functionality.

    Maybe option to run the php code directly as a php and not involve sql and the db? Run as a MU plugin?

    Plugin Author ImageShea Bunge

    (@bungeshea)

    Snippets run on the plugins_loaded hook – the execution order has nothing to do with the database or SQL code.

    The earliest time that snippets could possibly be loaded is when the Code Snippets plugin itself is loaded, which would be after ACF and ACF extended have already been loaded (because they would come earlier in the plugin load order).

    Thread Starter ImageRookie

    (@alriksson)

    Okay but there is no way to execute it before ACF and ACFE?

    Plugin Author ImageShea Bunge

    (@bungeshea)

    If ACF does not wait to run its code on an action or filter hook and runs it as soon as the plugin is loaded, then unfortunately it may not be possible.

    I’d need more information on what you are actually trying to do to give a better answer. Currently, I’m finding it difficult to determine exactly what that is from what you’ve said.

    Thread Starter ImageRookie

    (@alriksson)

    ACFE is acf extended separated plugin.

    Sure this is what I try to do https://1fix.io/blog/2016/02/05/parent-from-another-cpt/

    But it’s not super stable as ACFE loads the CPT before I can alter the url slug.

    Hello guys,

    ACF Extended developer here. I just wanted to let you know that the ACF Extended: Dynamic Post Types UI register post types on the init hook at the priority of 10.

    I don’t know what’s not working with post types here, but in the tutorial you provided, they use the init hook at the same priority of 10 to alter rewrite rules:

    add_action( 'init', 'my_add_rewrite_rules' )

    The thing is that in the tutorial, they most likely copy paste all the codes snippets together in the functions.php file. Starting with add_action( 'init', 'my_register_cpt' ) then with add_action( 'init', 'my_add_rewrite_rules' ).

    This will hook on init in the right order: register the post type first, then alter rewrite rules. Both on priority 10, but still in the good order in that priority.

    As you mix two plugins to hook that init (ACF Extended to register post type & Code Snippets to alter rewrite rules), there’s probably some order problem within that init hook priotity 10.

    You should try to set an higher priority on the rewrite rule function. Something like: add_action( 'init', 'my_add_rewrite_rules', 20 ).

    Hope it helps!

    Have a nice day.

    Regards.

    Thread Starter ImageRookie

    (@alriksson)

    @hwk-fr Thanks for the extra explnataiton.

    In my case I’ve tried to set the priority to 99 both in the rules and within code snippet which have a priority setting per snippet as well.

    The issue is it execute afteer ACFE. But I made it works once but I can not replicate it. So for that it do not feel safe to work, but I do not want to alter it as it works and havee done so for a couple of months.

    Couldn’t it be tat code snippet execute after ACFE init? As it sends sql query to execute php which happeens after ACFE?

    Hello,

    My answer was a guess. I don’t know what is the problem, so I can’t debug it. You’re kinda vague and don’t really give much information. What is the problem? Is it the Rewrite Rules in the tutorial that is not working, like I said earlier? or something else? You need to be very specific to help us help you.

    It doesn’t matter if code snippet is executed before/after ACFE or if there is a SQL query. It will work if the rewrite rule function is correctly hooked on init, after the post type registration (if rewrite rule is the actual problem).

    Regards.

    Thread Starter ImageRookie

    (@alriksson)

    Sure sorry.

    What is not working is the rewrite of url slug and sometimes the drop down meta box.

    The url slug isn’t always rewritten as it should once I try to set it up today again I should become:

    parent-cpt/%parent-cpt-page%/%cpt-page%

    But what happens is:
    parent-cpt/%parent-cpt-page%/%cpt-page%/%cpt-page% or
    parent-cpt/%parent-cpt-page%/%cpt%/%cpt-page%

    And with the one working I also have to add the same rwerite rule within ACFE CPT as it doesn’t work at all otherwise.

    So in rewrite arguments and slug I have parent-cpt/%parent-cpt-page%

    Hello,

    Okay. And when you paste the tutorial code in functions.php it works, but not in Code Snippets? Right?

    Regards.

    Hello,

    I tested the tutorial on a blank new WordPress install, and everything works fine when using functions.php only but also when using ACF Extended: Post Types + Code Snippets. The problem come from your side. Possible causes:

    1. Post Type Registration

    Make sure you correctly registered the post types. Remember, you’re supposed to follow the tutorial code. In their code they don’t alter/change the post type “Rewrite Slug” argument as you did. You should leave it disabled. See screenshot: https://i.imgur.com/Ovrd3im.jpg

    'label'                 => __( 'Course', '1fix' ),
    'hierarchical'          => true,
    'public'                => true
    
    'label'                 => __( 'Lesson', '1fix' ),
    'hierarchical'          => false,
    'public'                => true
    

    You should register your post types that way, and not change other default settings, unless you specifically know what you’re doing.

    Also please keep in mind that you have to “Flush Permalinks” everytime you make a change when dealing with post types URLs. Just visit the admin page “Settings > Permalinks” everytime you change the code/post type declaration. This will flush permalinks.

    2. Rewrite Rules function priority

    Please make sure to set a high priority on the hook add_action( 'init', 'my_add_rewrite_rules', 20 ). In this example 20.

    3. Code Modification

    Maybe you changed the code to fit your needs (change post type names, variables etc…), and broke something on the way. I would advise to clear your changes, and restart from the beginning. If you’re not too confident, you can ask some help on WP development forum or Stack Overflow.

    My Test Install export

    ACF Extended – Post Types:
    https://gist.github.com/acf-extended/f3fdf594789457dc86c73c1c6da8bc05#file-acfe-export-post-types-course-lesson-2020-08-16-json

    Code Snippets:
    https://gist.github.com/acf-extended/f3fdf594789457dc86c73c1c6da8bc05#file-courselesson-code-snippets-json

    Hope it helps!

    Regards.

    Plugin Author ImageShea Bunge

    (@bungeshea)

    Thank you for all your assistance with this @hwk-fr, really appreciate it.

    Thread Starter ImageRookie

    (@alriksson)

    Thanks guys and thanks @hwk-fr for debugging, I will try to catch up on this ticket soon.

    As from last time I tried to test different priorities and the reasong for rewrite slug if I remember it was due to otherwise it didn’t do anything or I face douplicate cpt folders in the slug /cpt/cpt/page/. But I will look into it and let you guys know.

    Many thanks ACFE is great! 😀

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

The topic ‘code snippet execute after plugin code is executed’ is closed to new replies.