2

I need to include plugin in my WordPress theme without plugin activation. I have these lines:

include_once dirname( __FILE__ ) . '/math/class-constrained-array-rounding.php';

include( plugin_dir_path( __FILE__ ) . 'class.jetpack-user-agent.php');

and

wp_enqueue_script( 'tiled-gallery', plugins_url( 'tiled-gallery/tiled-gallery.js', __FILE__ ), array( 'jquery' ) );

wp_enqueue_style( 'tiled-gallery', plugins_url( 'tiled-gallery/tiled-gallery.css', __FILE__ ), array(), '2012-09-21' );

I think I need to use function get_template_directory_uri() instead of plugin_dir_path to include these files, but I'm confused.

I'm new in WordPress. Can somebody suggest a solution to this problem? :)

  • 1
    The Question would be why? Please give more details. Maybe another way would be better... – kuchenundkakao Sep 5 '14 at 6:10
  • Thank you, I create theme for a client and I need to include plugin for WP gallery. It should be in theme files, without plugin activation. – Aineko Sep 5 '14 at 10:08
  • Something else to bear in mind: plugins are loaded earlier than themes are which gives them a chance to hook into more filters etc. during start up. I don't think there's a problem with those, but in general there's a chance a plugin won't work properly when loaded later with a theme. – Rup Sep 5 '14 at 17:05
4

WP_PLUGIN_DIR.'/pluginName/' will give you the absolute path to the directory of the plugin.

EDIT After comment

Plugins are plugins, theme files are theme files. Lets not confuse the two. You cannot copy a plugin in the theme directory - because stuff does not work like that.

After careful editing you can include the plugin as part of your themes options.

Lets make an example:

  1. I want to include the hello dolly plugin in my theme. So I copy hello.php into my themes directory like so: themes/mytheme/include/plugins/hello.php.

  2. In order to load this file ("plugin" - but its not really a plugin anymore) I need to require_once the hello.php file like so:

    define('mytheme_inc_path', TEMPLATEPATH . '/includes/');
    define('mytheme_inc_url', get_template_directory_uri(). '/includes/');
    require_once mytheme_inc_path. 'plugins/hello.php';
    

    a. If I have a more complex "plugin" then the code will be like this:

    require_once mytheme_inc_path. 'plugins/myplugin/myplugin.php';
    // because the plugin deserves its own directory
    

    Within the myplugin.php you will enqueue the required scripts and styles like so:

    function mytheme_plugin_scripts() {
        wp_enqueue_script('tiled-gallery',
            mytheme_inc_url . 'tiled-gallery/tiled-gallery.js',
            array('jquery')
        );
    
        wp_enqueue_style('tiled-gallery',
            mytheme_inc_url . 'tiled-gallery/tiled-gallery.css',
            array(), '2012-09-21');
    }
    
    add_action('wp_enqueue_scripts', 'mytheme_plugin_scripts');
    

Caution

  • Be sure to remove unwanted 'plugin' hooks (filter, activation/deactivation or otherwise).
  • Be sure to remove all other actions except those that you really require

Conclusion

If you did some research in script/style enqueue how-to's you'd know that plugins (that are stored in wp-content/plugins or wp-content/mu-plugins use a bit different functions to retrieve absolute/relative path and uri. Like so:

Function Reference/wp enqueue script

  • Thank you, but I need to include plugin files in my theme files. Plugin should be a part of my theme. I guess I should use get_template_directory_uri() instead of plugin_dir_path, but I can't write it in a correct way, I'm new in php. Can you help me with this please? – Aineko Sep 5 '14 at 10:12
  • @Aineko how about now? – aifrim Sep 5 '14 at 14:15
  • Oh thank you very much, aifrim! Now it's all ok. It works! – Aineko Sep 6 '14 at 1:25
  • Then you should chose it as the right answer =D You are welcome. – aifrim Sep 6 '14 at 7:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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