Tag Info

Hot answers tagged

19

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


11

First rule of thumb: do not deregister core-bundled scripts and replace with other versions, unless you are absolutely certain that no Theme, Plugins, or core itself will break due to the version change. Really, unless you absolutely need an alternate version of a core-bundled script, just use what is bundled with core. Second, I strongly recommend hooking ...


11

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


6

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


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

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

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

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

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


3

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


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

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


2

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


2

Another great way of accomplishing something like this would be taking advantage of WordPress Custom Post Types. You could set up 3 different types of features for easy use in the WordPress back-end. First, Set up the Custom Post Types To do this, open up your theme's functions.php file and include something like this: <?php function ...


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

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

Hope this helps, look up the codex for wp_enqueue_scripts for more information. Dont use init to enqueue. Use wp_enqueue_scripts for front-end stuff and admin_enqueue_scripts for admin side. You can use init to register scripts though. The hook wp_enqueue_scripts only fires on the front-end (and not on the log-in page) - so you don't have to check ...


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


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


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

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


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

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

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

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


1

if (function_exists('load_my_scripts')) { function load_my_scripts() { if (!is_admin()) { wp_deregister_script( 'jquery' ); wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js', array(), null, false); wp_enqueue_script('jquery'); } } } add_action('init', 'load_my_scripts'); ...



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