Hot answers tagged

41

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 ...


22

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 ...


6

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 ...


6

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(){ ...


5

$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


5

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. ...


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') ?>


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

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 ...


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

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().


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

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

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 ...


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

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


4

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. ...


4

This should work: add_action( 'woocommerce_init', 'remove_wcpgsk_email_order_table' ); function remove_wcpgsk_email_order_table() { global $wcpgsk; remove_action( 'woocommerce_email_after_order_table', array( $wcpgsk, 'wcpgsk_email_after_order_table' ) ); }


4

When using plugin_dir_path() like you do: plugin_dir_path( __FILE__ ); It does return something like: /var/www//wordpress/wp-content/plugins/your-plugin/ So it adds a trailing slash, because as the documentation states: It is a wrapper for trailingslashit( dirname( $file ) );. So remove the slash before the file in your calls: require_once ...


3

A neat solution found in the codex So if you are looping thru custom posts, you can do this: foreach ($custom_posts as $custom_post) { set_query_var( 'my_post', $custom_post ); get_template_part( 'content', 'part' ); } And in that template itself, you will automatically get a $my_post.


3

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. ...


3

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( ...


3

You can... Load the file into the file where you want to display the 'hey username' message: <?php include(TEMPLATEPATH .'/check-user-hello.php'); ?> . Then in that file "check-user-hello.php" You need to put this code <?php if ( is_user_logged_in() ) { global $current_user; get_currentuserinfo(); echo 'Hey ' . $current_user->display_name; ...


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

I'm afraid not. If in codex isn't something you would like to know, try to follow the link to the source and have a look yourself to the code and try to manage it out. I had a look and the get_template_part function is defined as below: function get_template_part( $slug, $name = null ) { do_action( "get_template_part_{$slug}", $slug, $name ); ...


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

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 ...



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