Hot answers tagged functions
12
Updating the posts
$all_posts = get_posts(
'posts_per_page' => -1,
'post_type' => 'post'
);
foreach ( $all_posts as $single ) {
wp_update_post( array(
'ID' => $single->ID,
'post_title' => to_title_case( $single->post_title ) // see function below
));
}
Converting a string to "Title Case"
And, while not ...
9
Whenever you find a piece of code without clear installation instructions it is probably a plugin. The example you gave is a good one, because that is the most common case:
add_action('template_redirect', 'remove_404_redirect', 1);
function remove_404_redirect() {
// do something
}
To use such a snippet, put it into a plugin:
Create a new file, name it ...
8
Not everyone will follow convention, so you can be assured if you are copy-pasting then you are getting a mix-and-match approach from people who do it "right" and do it "wrong" and sometimes the difference between right and wrong is a matter of opinion, lets not forget.
Also this applies to NOT only Syntax Style but also Best Practice methods for using ...
8
Your question is a bit specific if you "only" want to automatically import some posts/pages. There are other ways to do this then using a XML export file.
If you have text-only posts, then you should use LOAD DATA INFILE. At first you have to export your posts.
global $wpdb, $wp_filesystem;
$tables = array(
'posts' => array( 'posts', ...
7
Like it was mentioned before removing the shortcode and re-adding it is not the compatible with other plugins modifying galleries so instead you use the post_gallery filter hook and the same code from the gallery_shortcode function but with your own modification for example, I've commented out the parts you don't want:
function ...
7
You can use the following instead,
if (is_singular('post')) {
//your code here...
}
Where by is_singular is the WordPress API conditional function for testing for the existence of a post type. You can also pass an array of post types if you wish.
http://codex.wordpress.org/Function_Reference/is_singular
7
Actions & Filters
The imho best way is to use an action to bring plugin functions into themes.
Example #1
Here's a little plugin to test this.
<?php
/** Plugin Name: (#68117) Print Hello! */
function wpse68117_print_hello()
{
echo "Hello World!";
}
add_action( 'wpse68117_say', 'wpse68117_print_hello' );
Inside the theme:
<?php
/** ...
7
I wrote a plugin for this -- that needs some updating, but it should still work fine.
Essentially you hook in some place late after WordPress knows what page is requested (like template_redirect), use one of the conditional functions and either use $wp_query->set_404() or just redirect the user to a page of your choice.
To disable categories, tags, ...
7
Never de-register core-bundled scripts in the WP-Admin. You shouldn't do it on the front end, either, unless you really, really know what you're doing. But especially in the WP-Admin, just use the core-bundled scripts.
Also, when you use core-bundled jQuery UI, WordPress already knows that jQuery is a dependency.
Just change the first callback to this:
...
7
You don’t have to remember very much. Any decent IDE will help you with auto-complete and an overview of the parsed PHPDoc.
Example from Eclipse:
After typing add_ you get a list of matching function names and constants, a description and an explanation of the parameters … if there is a useful PHPDoc block.
And when you create a new object you get the ...
6
This function inserts your ad code after the specified paragraph.
add_filter( 'the_content', 'wpse_ad_content' );
function wpse_ad_content( $content ) {
if( !is_single() )
return $content;
$paragraphAfter = 2; //Enter number of paragraphs to display ad after.
$content = explode ( "</p>", $content );
...
6
I would keep the primary content field and add a metabox + a secondary instance of the wp editor (using the handy wp_editor function).
Custom field values are stored in the database as LONGTEXT, so they can handle just about anything you wish to throw at them.
A class to wrap everything up. There's a few constants here we'll use later.
<?php
class ...
6
Don't ask me way but i actually have a function to count hooked functions to a tag
/**
* count_hooked_functions
* @author Ohad Raz
* @param string $tag hook name as string
* @return int the number of hooked functions to a specific hook
*/
function count_hooked_functions($tag = false){
global $wp_filter;
if ($tag){
if ...
6
That twentyeleven_setup function, and others like it, are pluggable - meaning that they can be overridden by a child theme. The child theme's version of a function with that same name will be parsed first, so the parent theme's version will not be run at all.
In TwentyTwelve some functions are not pluggable, but instead are attached to hooks. By attaching ...
6
Our journey starts here with the WP_Customize_Background_Image_Control class, which is a WP_Customize_Image_Control.
I'd imagine offering these built-in backgrounds in a new tab alongside the existing Upload New and Uploaded tabs. There are at least two ways of achieving the following: either creating your own modified class based off of the ...
6
Well, I'd say that you need a custom plugin. All the rationale is in this Q&A:
Where to put my code: plugin or functions.php?
Also related:
Where do I put the code snippets I found here or somewhere else on the web?
Create a Functionality Plugin Instead of Using Functions.php
And answering to the Question, create the following file ...
6
Use a plugin for that. Hook into after_setup_theme and check if the current theme is the correct child theme:
add_action( 'after_setup_theme', 'common_child_theme_functions' );
function common_child_theme_functions()
{
if ( 'Twenty Twelve' !== wp_get_theme()->parent() )
return;
// do your work
}
wp_get_theme() returns a WP_Theme ...
6
There are a couple of problems with your code:
You have to close the PHP context, if you want to output plain HTML: function foo(){ ?><strong><?php }
Don’t repeat yourself. Always store repeating values in variables or functions. Writing <a href more than once is a bug.
Do not use get_the_title() in attributes. Use the_title_attribute( array ...
5
Use is_home instead, as is_page_template will not work for home.php as its technically not a page template in the traditional sense.
add_action('template_redirect', 'are_we_home_yet', 10);
function are_we_home_yet(){
if ( is_home() ) {
//your logic here
}
}
Revised:
add_action('template_redirect', 'are_we_home_yet', 10);
function ...
5
No, it will fail. There are literally thousands of mobile clients, and not all send a usable User-Agent header. There are better ways to adjust the site to small screens (which is not necessary the same as mobile!).
Use client side detection and offer a link to switch the view; save that option in a cookie.
I think the common misconception is that mobile ...
5
It is a function created to be used as a hook.
Inside the comments.php file is
<?php wp_list_comments('type=comment&callback=bones_comments'); ?>
So you can add whatever you want to the bones_comments() function and have it added to the comment output.
5
A long time ago WordPress did not put feed links into the head element automatically. Theme or plugin authors had to do that.
In 2009 automatic_feed_links() was introduced, a function that should be used in themes or plugins to let WordPress do the hard work.
One year later it was added to Twenty Ten, and it became a de facto standard.
Not much later ...
5
First, don't unregister and reregister jQuery. There's no reason to do that at all. Second, you're enqueuing on the wrong hook. Instead, use this in your functions file:
// Enqueue Scripts
function wpse78227_enqueue_scripts() {
wp_enqueue_script( 'jquery' );
}
add_action( 'wp_enqueue_scripts', 'wpse78227_enqueue_scripts' );
Now, to include that ...
5
You can try using wp cron functions, programing a feed import every 10 minutes or so, until you have nothing left in queue.
Example code:
<?php
register_activation_hook( __FILE__, 'wp1903_schedule_cron' );
function wp1903_schedule_cron() {
$counter = 0;
$feeds = array(
'http://example.com/feed1',
'http://example.com/feed2',
...
5
function remove_submenu() {
remove_submenu_page( 'edit.php', 'edit-tags.php?taxonomy=faq-topic&post_type=question' );
remove_submenu_page( 'edit.php', 'edit-tags.php?taxonomy=faq-tags&post_type=question' );
}
add_action( 'admin_menu', 'remove_submenu', 999 );
Please read the Codex. remove_submenu_page() need two parameters and the right ...
5
To exclude a category, you need to know its ID. You can then exclude it like this (please not the minus before the ID):
$args = array( 'posts_per_page' => 4, 'cat' => -the_category_id_to_put_in);
The WP Query class reference has more information if you need it: http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
4
Drop this in your functions.php or a custom plugin.
function wpse_57621_alo_tidy()
{
if ( $page_id = get_option( 'alo_em_subsc_page' ) ) {
if ( ! is_page( $page_id ) )
remove_action( 'wp_head', 'alo_em_ajax_js' );
}
}
add_action( 'wp', 'wpse_57621_alo_tidy' );
It's attached to the wp hook, which runs just after the request has ...
4
If you are deleting code from the main WordPress files, this means that you know your way around a whole bunch of PHP files, so it's time now to learn how to do things without touching core files.
Side note to the hook presented in the Question:
the filter global_terms_enabled only works for Multisite (/wp-includes/functions.php, line 3006).
In many cases, ...
4
You could try something like (http://codex.wordpress.org/Function_Reference/get_post_type) :
to check if it's not a specific post-type :
if ( is_single() && 'portfolio' != get_post_type() ) {
// DO STUFF
}
or to check if it's a post and not a post-type :
if ( is_single() && 'post' == get_post_type() ) {
// DO STUFF
}
4
It's because you're not registering them as a media type. Every upload is a WordPress post of the attachment type.
To start, it would be something like this:
$file_name = 'Some Name';
$file_path = '/path/to/uploads/2012/08/04/newfile.jpg';
$file_url = 'http://url/to/uploads/2012/08/04/newfile.jpg';
$wp_filetype = wp_check_filetype($file, null);
$attachment ...
Only top voted, non community-wiki answers of a minimum length are eligible

