Tag Info

Hot answers tagged

27

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


12

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


7

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

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

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

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

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


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

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


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

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

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


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

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


2

Wordpress has its own connection class, which can be used trough the $wpdb object. Is there a particular reason to use direct queries to the db, instead the wordpress functions (get_posts, get_post_meta, get_option)? Keep in mind that by doing this, you loose a lot of the wordpress tools, such as cache, filters, etc...


2

Each auto-draft gets its own ID, each revision, each nav item, page, custom post type … The actual ID of a post should be irrelevant, this is really just needed for the database and ugly permalinks. You cannot get the last 30 items by inspecting the post ID only. Install a REST API on the other site and a rule to get the last x items.


2

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


2

Use the built-in tool: the media library. There are two steps to do that: Allow uploading txt files, to enable the upload. Filter the AJAX action that inserts the HTML code. 1. Allow txt file upload This is pretty simple: filter upload_mimes and add your types to the existing list. add_filter( 'upload_mimes', 'extend_upload_mimes_for_txt' ); function ...


2

Another solution, that doesn't invoke page attributes: Explanation: To-Do List Go and download the RW_Meta_Box Library. I'm one of the authors/contributers. The Plugins Here you can see two small plugins (which I wrote especially for your question). Input The first one is a dependency of the RW Meta Box library. You just change what you need (User cap ...


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

In your main plugin file, use plugin_dir_path to define a constant that you can then use in all of your includes. see the example on the above codex page. // define the constant in your main plugin file define( 'MYPLUGINNAME_PATH', plugin_dir_path(__FILE__) ); then, to include your file: include MYPLUGINNAME_PATH . 'includeme.php';


2

When you call admin-ajax.php no query is being produced so is_page() or is_category() or any query based conditional tag will never return true. A better way would be to include your files inside the ajax callback, meaning something like this: add_action('wp_ajax_PAGE_ONLY_ACTION','PAGE_ONLY_Function'); function PAGE_ONLY_Function(){ include ...


2

There is my simple function solving variable problem. It's doing same thing as Wordpress do in get_template_part() function. Just copy and paste to function.php function getTemplatePart($slug = null, $name = null, array $params = array()) { global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID; ...


2

First you need site to be processed as PHP, since WP simply won't work otherwise. I think you can do it for non-.php extensions by tinkering with server config. THen see Integrating WordPress with Your Website in Codex. You can load WP core to required degree and use the functions. However I'd consider just migrating site to WP completely.


2

If I had a site where I wanted to do this I'd convert the layout to a wordpress theme, and then import the pages -- playing with the URL format so that they'd correspond. Creating a basic theme from existing HTML is pretty easy - see this tutorial for instruction: http://www.codeislove.net/2011/03/converting-html-layout-into-wordpress.html ...


2

The problem here is that you're not including yourtheme/includes/admin.php, you're actually including wp-admin/includes/admin.php, so pass a full path to the require statement rather than a relative one e.g.: require(get_template_directory().'includes/admin.php');



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