Tag Info

Hot answers tagged

38

Like MathSmath wrote, get_template() does not support the re-use of your variables. But locate_template() infact does no inclusion at all. It just locates a file for inclusion. So you can make use of include to have this working just like you expect it: include(locate_template('custom-template-part.php')); $var from your example can be used in the ...


16

In fact you can, I have a folder in my theme directory called /partials/ in in that folder I have files such as latest-articles.php, latest-news.php and latest-statements.php and I load these files using get_template_part() like: get_template_part('partials/latest', 'news'); get_template_part('partials/latest', 'articles'); ...


8

The best place to include code in theme is usually Functions File (functions.php) rather than template files (which can and often are overridden by plugins, child themes, etc). Depending on your specifics you can define constants in there or create wrapper function and hook it to some action firing at later stages of theme load. Also depending on ...


8

I've had trouble with this too (while trying to get a custom query to work with a template part). The short answer is: no, the template part doesn't automatically inherit custom vars the way a regular include does. Both get_template_part() and locate_template() eventually use the load_template() function to actually load the file (using a require). This ...


5

jQuery needs to be outside the wrapper like so: <script type="text/javascript"> (function($) { $(document).ready(function() { alert('hello?'); }); })(jQuery); </script> Edit: A better way would be: jQuery(document).ready(function($) { // $() will work as an alias for jQuery() inside of this function }); Also make sure any ...


5

The problem is that you are loading your script before jQuery has been loaded. Do not print scripts directly. You should (register and then) enqueue them using the provided API. jQuery is already a registered script, so you can just enqueue it (say on the admin_enqueue_scripts hook). However you only need to load jQuery if you are loading a custom ...


5

Both are acceptable but not recommended. Use locate_template() instead because a child theme can overwrite the loaded file then. Example: $found = locate_template( 'functions/my-custom-widget.php', TRUE, TRUE ); The first TRUE tells WordPress not only to search for the file but to load it actually. The second makes it a require_once call. The function ...


5

Use plugin_dir_path() to include executable files. plugins_url() returns the web address, that’s not what you need. <?php include(plugin_dir_path(__FILE__) . 'forms/admin_form.php') ?>


4

get_stylesheet_directory_uri() returns a value, it doesn’t print anything. So you have to use: echo get_stylesheet_directory_uri(); get_template_part() is just a wrapper for locate_template(). But the latter has one advantage: It returns the path of the file it has found. Try the following: $path = locate_template( 'sidebar-front.php', TRUE ); echo ...


4

add_action('wp_enqueue_scripts', 'no_more_jquery'); function no_more_jquery(){ wp_deregister_script('jquery'); } That will deregister jquery. But why wouldn't you want jQuery at all? If you mean to simply use your own, you should do it in that function, like this: add_action('wp_enqueue_scripts', 'no_more_jquery'); function no_more_jquery(){ ...


4

Long answer short: the absolute-best answer, for template-part files in a subdirectory, is to use locate_template() I would recommend referencing the stylesheet path for template file includes, so that those template part files can be easily over-ridden by Child Themes. So, ideally, you should use get_stylesheet_directory_uri(). Some ...


4

Thinking about it as including pages is a little off. Think about it as retrieving and including page's content. $page = get_page_by_title('About The Tests'); $content = apply_filters('the_content', $page->post_content); echo $content; Another way would be set up special sidebar for that and let client add/remove/content via widgets. But such can ...


4

If you have a lot of products, probably a custom sql query is a better option. I'd probably recommend following that route either way. Custom queries may be 'icky', but it'll lighten resources and be a darn slight quicker in most cases* I've written a function that does most of the labour, and may well come in handy in other scenarios (I've used the ...


4

If you just need that class included, and your script is located in the plugin directory, like /wp-content/plugins/pluginName/script.php, then you can do: require realpath('../../../wp-includes/class-phpass.php');


4

Variables have a certain scope. The PHP Manual explains that in detail. So when you set a variable you should know in which scope those are set. This depends on where you set them and how that file gets included. As Rarst already suggested, the function.php file is an ideal place as it gets included on the global space whenever your theme is active. Next ...


3

is_plugin_active() expects just the base name of the plugin as parameter: So use: is_plugin_active( 'woocommerce/woocommerce.php' ); The function will use the option 'active_plugins' which is a list of plugins paths relative to the plugin directory already. On a multi-site installation it will search in get_site_option( 'active_sitewide_plugins') too. ...


3

You need to have the script in a separate file (normally it would be filename.js; I suppose filename.php would work?). Then, you need to register and enqueue that script file, using wp_register_script() and wp_enqueue_script() e.g.: function mytheme_register_custom_scripts() { if ( ! is_admin() ) { $scriptsrc = get_stylesheet_directory_uri() . ...


3

Your functions.php doesn’t create output, so you should use locate_template(). Example: locate_template( 'php/functions.nav-menu.php', TRUE, TRUE ); You’ll find this function in wp-includes/theme.php. The first parameter is the file path relative to the theme root, the second tells WordPress to load it (or not), and the third to load it just once. Now a ...


3

Just my two cents for future references, a workaround at least in Wordpress 3.5 is to add the variable to $wp_query->query_vars. I needed my global _vk_errors inside a template part and just did $wp_query->query_vars['_vk_errors'] = $_vk_errors; before calling get_template_part().


3

Why don't you use a simple function with an argument to achieve that, the code is something like this: function wpse63585_event_list( $fresh = true ) { echo '<ul class="event-items">'; $yesterday = time() - 24*60*60; $compare = $fresh ? '>' : '<'; $args = array( 'post_type' => 'wr_event', 'posts_per_page' ...


3

$args = array( 'post_type' => 'testimonials', 'posts_per_page' => 4, 'orderby' => 'post__in', 'post__in' => array(883, 563, 568, 106); ); Using post__in within the orderby value it will honour the order of the array of IDs passed in post__in


3

Most of the time, if you are doing things right you don't have to include Core files. If you are doing things right they will already be loaded for you. In those case where they aren't loaded, trying to load files with "directory traversal" paths is the wrong way. The Core constants ABSPATH and WPINC should give you the base path the the includes directory. ...


2

No, you cannot include PHP files in a theme the way you indicated. To see why, all we have to do is exercise some common sense and basic critical thinking: You would be doing a remote request to a remote server include cannot do this If by some miracle it worked, you would have a major security exploit as I could then do something like this: include( ...


2

You ask: How does that work ? As far as I can read it, it only specifies not to rewrite certain urls (using -), These files are included within the WordPress PHP scripts, so there's no need to access them in the browser, but notice the rewrite flags. Here's some information on the F, L and S flags from the Apache docs: Using the [F] flag causes ...


2

suppose you have this cenario: Your wordpress instalation is hosted on 'www' root folder as your site, and your header.php is on 'www/includes' folder. In you wordpress theme, just add <?php include('includes/header.php'); ?> When WordPress reads theme files, it uses the root path.


2

There is not quite any clean way to query for "this set of conditions oh and also something completely unrelated as well". There are multiple ways to handle it, few of them being: query two things and deal with combining in template or around (easy, pain with pagination) hook into WP_Query and mess with SQL (easy... if you are good with SQL) query or ...


2

When including files in functions.php, you need to reference the correct filepath, using get_template_directory(): include( get_template_directory() . '/newfile.php' );


2

require_once (PHP documentation) will check if the file has already been included, and if so, not include (require) it again. This check will take more time. If you know what you're doing (including), then you should ditch the _once part and save time. EDIT: you won't see the difference with only a few files. But if you don't include lots of files, ...


2

is_plugin_active() only available inside the admin area. You need to include the core plugin.php file in front end to use this function. From WordPress documentation NOTE: defined in wp-admin/includes/plugin.php, so this is only available from within the admin pages, and any references to this function must be hooked to admin_init or a later action. ...


2

You're making something completely wrong. Header comment In your main file, you need the following comment on top (ex. taken from Contact form 7): <?php /* Plugin Name: Contact Form 7 Plugin URI: http://contactform7.com/ Description: Just another contact form plugin. Simple but flexible. Author: Takayuki Miyoshi Author URI: ...



Only top voted, non community-wiki answers of a minimum length are eligible