Title: code snippet execute after plugin code is executed
Last modified: July 17, 2020

---

# code snippet execute after plugin code is executed

 *  [Rookie](https://wordpress.org/support/users/alriksson/)
 * (@alriksson)
 * [5 years, 10 months ago](https://wordpress.org/support/topic/code-snippet-execute-after-plugin-code-is-executed/)
 * 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 [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * (@bungeshea)
 * [5 years, 10 months ago](https://wordpress.org/support/topic/code-snippet-execute-after-plugin-code-is-executed/#post-13139062)
 * 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 [Rookie](https://wordpress.org/support/users/alriksson/)
 * (@alriksson)
 * [5 years, 10 months ago](https://wordpress.org/support/topic/code-snippet-execute-after-plugin-code-is-executed/#post-13143781)
 * 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 [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * (@bungeshea)
 * [5 years, 10 months ago](https://wordpress.org/support/topic/code-snippet-execute-after-plugin-code-is-executed/#post-13143984)
 * 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 [Rookie](https://wordpress.org/support/users/alriksson/)
 * (@alriksson)
 * [5 years, 10 months ago](https://wordpress.org/support/topic/code-snippet-execute-after-plugin-code-is-executed/#post-13146803)
 * Okay but there is no way to execute it before ACF and ACFE?
 *  Plugin Author [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * (@bungeshea)
 * [5 years, 10 months ago](https://wordpress.org/support/topic/code-snippet-execute-after-plugin-code-is-executed/#post-13260621)
 * 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 [Rookie](https://wordpress.org/support/users/alriksson/)
 * (@alriksson)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/code-snippet-execute-after-plugin-code-is-executed/#post-13261139)
 * 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/](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.
 *  [Konrad Chmielewski](https://wordpress.org/support/users/hwk-fr/)
 * (@hwk-fr)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/code-snippet-execute-after-plugin-code-is-executed/#post-13262125)
 * 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 [Rookie](https://wordpress.org/support/users/alriksson/)
 * (@alriksson)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/code-snippet-execute-after-plugin-code-is-executed/#post-13262856)
 * [@hwk-fr](https://wordpress.org/support/users/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?
 *  [Konrad Chmielewski](https://wordpress.org/support/users/hwk-fr/)
 * (@hwk-fr)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/code-snippet-execute-after-plugin-code-is-executed/#post-13263532)
 * 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 [Rookie](https://wordpress.org/support/users/alriksson/)
 * (@alriksson)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/code-snippet-execute-after-plugin-code-is-executed/#post-13263717)
 * 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%`
 *  [Konrad Chmielewski](https://wordpress.org/support/users/hwk-fr/)
 * (@hwk-fr)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/code-snippet-execute-after-plugin-code-is-executed/#post-13263750)
 * Hello,
 * Okay. And when you paste the tutorial code in `functions.php` it works, but not
   in Code Snippets? Right?
 * Regards.
 *  [Konrad Chmielewski](https://wordpress.org/support/users/hwk-fr/)
 * (@hwk-fr)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/code-snippet-execute-after-plugin-code-is-executed/#post-13263917)
 * 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](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](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](https://gist.github.com/acf-extended/f3fdf594789457dc86c73c1c6da8bc05#file-courselesson-code-snippets-json)
 * Hope it helps!
 * Regards.
 *  Plugin Author [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * (@bungeshea)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/code-snippet-execute-after-plugin-code-is-executed/#post-13267605)
 * Thank you for all your assistance with this [@hwk-fr](https://wordpress.org/support/users/hwk-fr/),
   really appreciate it.
 *  Thread Starter [Rookie](https://wordpress.org/support/users/alriksson/)
 * (@alriksson)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/code-snippet-execute-after-plugin-code-is-executed/#post-13380598)
 * Thanks guys and thanks [@hwk-fr](https://wordpress.org/support/users/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.

 * ![](https://ps.w.org/code-snippets/assets/icon.svg?rev=2148878)
 * [Code Snippets](https://wordpress.org/plugins/code-snippets/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/code-snippets/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/code-snippets/)
 * [Active Topics](https://wordpress.org/support/plugin/code-snippets/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/code-snippets/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/code-snippets/reviews/)

 * 14 replies
 * 3 participants
 * Last reply from: [Rookie](https://wordpress.org/support/users/alriksson/)
 * Last activity: [5 years, 9 months ago](https://wordpress.org/support/topic/code-snippet-execute-after-plugin-code-is-executed/#post-13380598)
 * Status: not a support question