1

I know how to include my own functions by writing them into the theme's functions.php and enqueuing them, or by including a php file containing all that in functions.php.

But that is always bound to the used theme. If I switch to another theme, I always have to copy that code to the new theme's functions.php

Is there any way around that - a place/file where I can define and enqueue scripts that remain valid across all themes? (without loosing it when WP is updated)?

1 Answer 1

2

This is not quite simple (as copying and pasting into new functions.php :D), but you can create a plugin that will load this every time.

A (very) good and simple guide can be found here.

Addition by Original Poster (johannes)

I created a plugin for that:

  1. I created a simple php file and put it into wp-content/plugins. It looks basically like this (i.e. it contains the plugin name, description, author etc. and the function I want to use in my site across all themes):

    <?php
    /**
     * Plugin Name: Function-Includer
     * Plugin URI: my URI
     * Description: my description
     * Version: 1.0
     * Author: johannes
     * Author URI: my URI
     */
    
    [... code of my function ...]
    
    ?>
    
  2. Then I went into the WP admin backend, to the plugin page, where that plugin is listed as "Function-Includer" and activated it.

  3. Done - the function is available everywhere in my site now.

7
  • 1
    May you please elaborate on how this guide would help? Sep 21, 2016 at 13:06
  • Wow, this wasn't even all that hard in my case - first I hesitated, but then I managed creating that plugin in a few minutes, and it works :-)
    – Johannes
    Sep 21, 2016 at 13:35
  • 1
    Detailing what you did as an answer or an edit to the accepted answer would improve this Q&A for others. Sep 21, 2016 at 13:39
  • @AndyMacaulay-Brook okay, I just did that
    – Johannes
    Sep 21, 2016 at 13:52
  • do you mind sharing the code? Without complete code I don't see how this answer can help anybody!!
    – Tara
    Sep 21, 2016 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.