A function is a sequence of program instructions that perform a specific task. It is packaged as a unit that can be used wherever the according task should be performed.

learn more… | top users | synonyms (2)

220
votes
108answers
101k views

Best Collection of Code for your functions.php file [closed]

Please vote on the question and any answers you find useful by clicking on the UP arrow on the left hand side of the question or answer. As with many others who are now viewing this post, I have been ...
18
votes
2answers
6k views

Between functions.php, widgets and plugins, which is loaded first?

Customer asks if a specific carousel plugin he uses can be widgetized. That means i should create a widget inside functions.php which calls the plugin's function. That means that the plugin's code has ...
17
votes
3answers
24k views

Changing Admin Menu Labels

I have spent the last day using the functions.php file to fully customize WordPress for my client sites. I am amazed at how much I have been able to accomplish and how much easier it will make things ...
15
votes
4answers
257 views

How long does a deprecated function live in core?

I'm curious to find out the process/philosophy behind removing deprecated functions from core. The Codex says this: These functions below have been deprecated. That means they have been replaced ...
14
votes
1answer
8k views

How to override parent functions in child themes?

I've been reading around and trying to figure out how to do this, but for some reason I can't seem to override parent functions in my child theme. I'm using TwentyTen as a parent - can anyone tell me ...
13
votes
3answers
2k views

Where do I put the code snippets I found here or somewhere else on the web?

Many posts here or somewhere else contain code, but they do not say where to put it. Example: I have found this post: How do I turn off 301 redirecting posts (not canonical)? I'm a newbie with PHP. ...
12
votes
6answers
16k views

remove empty paragraphs from the_content?

Hey guys, I simply want to prevent the creation of empty paragraphs in my wordpress post. That happens quite often when trying to manually space content. I don't know why this doesn't take effect? ...
12
votes
4answers
12k views

What's the difference between home_url() and site_url()

My understanding is that site_url() returns the location where the wordpress core files are. So, if my blog is hosted at http://example.com/blog then site_url() returns http://example.com/blog But ...
11
votes
7answers
29k views

How to include PHP files in plugins the correct way

My problem is when on the main plugin file i include a php file something like this: include(WP_PLUGIN_URL . '/wordpress-group-buying/ipn/paypal-ipn.php'); //or include_once(WP_PLUGIN_URL . ...
11
votes
2answers
801 views

How to make a image-size selected by default in Media upload - WP v3.5

Bare with me. I want to have a custom image size selected by default in the Media upload popup page. In Wordpress v3.4.2 and prior, this elegant code worked fine: function ...
10
votes
4answers
16k views

How to manually fix the WordPress gallery code using PHP in functions.php?

It's been talked many times that Wordpress outputs some really bad code for the built-in gallery function. This is the core code responsible for the gallery output (in /wp-includes/media.php): ...
10
votes
1answer
670 views

Is there a is_user_logged_in() for multisite?

Is there a is_user_logged_in() function for Wordpress MU / Multisite (3.0+) where I can add in the network site ID like is_user_logged_in(2) where 2 would be the site_id? Update: After a little ...
9
votes
1answer
2k views

Using classes instead of global functions in functions.php

In many themes I've seen (including TwentyEleven) and in the examples I've found online, when building the functions.php file for a theme all functionality is declared in a global scope. To clarify, ...
8
votes
3answers
1k views

How to change the case of all post titles to “Title Case”

I am helping my father with his WordPress website. It has over 1,700 posts with TITLES IN UPPERCASE. We'd like to change these to "Title Case" in the database (possibly using this PHP script). The ...
8
votes
2answers
187 views

Displaying the number of updates available in the Admin area

I am having an issue finding out how to display the number of plugins/updates available to call somewhere else other than the admin header. I found the function wp_get_update_data should be what I ...
8
votes
1answer
4k views

Redeclare/Change Slug of a Plugin's Custom Post Type

Is it possible to redeclare/change the slug of a plugin's existing custom post type (without simply editing the plugin)? That is, if Plugin X creates a custom post type with the slug /uncookedtoast/, ...
8
votes
1answer
714 views

Mega Menu Walker

I'm trying to create a mega menu walker. Unfortunately, walkers completely escape my coding knowledge. I could really use some help getting it working. Here's the features I need: Wrap the ...
8
votes
2answers
2k views

Programatically add options to “add new” custom field dropdown

How can I add pre-defined options to the "add new" custom field dropdown? Here's two examples of automatically adding and showing new custom fields: WordPress: Adding Default Custom Fields on New ...
7
votes
4answers
361 views

Add image size if page template

I'm building a members website with WordPress Multisite. Is it possible to restrict how many images are being generated depending on the selected template? I have tried the following lines of code ...
7
votes
4answers
2k views

How to load parent_theme functions.php before child_theme?

I got the problem that i have to load my parent theme's functions.php file before my child theme's functions.php file loads. This is needed for the setup & init procedure. I looked at the hooks ...
7
votes
2answers
321 views

Get rid of Wordpress category, tag and author archives?

I have searched the net with no success on what should be an easy thing. I have a highly customized blog with pages, sub pages and posts. While I rely heavily on the use of categories and tags I ...
7
votes
1answer
158 views

Shared functionality in plugins and themes

I recently started to develop plugins and themes and I found that I need to use several functions of on both. Sometime I think about to check if function / class exist before declared as said on this ...
7
votes
2answers
171 views

Remove Actions/Filters added via Anonymous Functions

That's a freaking bad practice I must say. Spent last two hours finding a solution to remove actions and filters added via Anonymous functions. This is the code used on a Parent Theme, and I need ...
7
votes
2answers
1k views

Remove “Insert from URL” link in Media upload - WP 3.5

How do I remove the Insert from URL link in the new Wordpress 3.5 Add Media popup page? In earlier versions of Wordpress, this worked fine: // removes URL tab in image upload for post function ...
7
votes
3answers
623 views

Order posts (across the whole site) by metadata date

My custom 'Home' page is set to sort my custom posts via the custom date field with this (updated to be custom named) code in functions.php: // sort order for home page add_action('wp', ...
6
votes
4answers
265 views

Cleaning up WordPress to improve performance?

Looking over the current WordPress codebase it's clear that functions are over-used. My count is over 8,100 functions/methods are defined inside the WordPress codebase. That is terrible, and a ...
6
votes
4answers
3k views

Import Wordpress XML File from Within Functions.php

I am developing a theme which has a different method of adding in content and so, the default install of Wordpress won't show any content because of this. I was wondering if it is possible to ...
6
votes
2answers
149 views

Memorizing syntax

I hope the following question is acceptable here. I rely entirely on copy/paste for most WP functions, however I guess WP veterans don't. Is it important to know by heart the common WP syntax, and if ...
6
votes
2answers
4k views

How to change default position of WP meta boxes?

Im wondering if there is a way to change the default position of Wordpresses meta boxes such as "featured image" for custom post types without having to drag them manually? Example:
6
votes
1answer
155 views

How to redirect to post if search results only returns one post

I want to send visitors to my search.php after a search to display list of posts. If there is only one search result, user can directlyto the post in question(something like GOOGLE's I am Feeling ...
6
votes
1answer
1k views

What is the difference between wp_register_sidebar_widget and register_widget?

What's the difference between them and when should we use each one ? I'm using wp_register_sidebar_widget right now and it's working fine but I've seen a lot of tutorials online on how to create a ...
6
votes
1answer
1k views

Set media upload attachment link to none and hide it in WP v3.5

In the Media upload popup page, how do I set the attachment link to none and hide it in Wordpress version 3.5? The following filter worked fine in earlier versions of WP: function ...
6
votes
2answers
2k views

How to add a custom metabox to the Menu Management admin screen?

Although I am very thankful to the wordpress core team that they have finally integrated native menu management capabilities I get frustrated with some key elements which I would like to change. I ...
6
votes
2answers
194 views

How do I use the WP image functions in a page template?

Wordpress has tons of mature code for image editing on the admin side, but I would like to provide a form inside of a custom page template for my visitors to drag and drop, crop and save a thumbnail ...
6
votes
1answer
185 views

Gettext details

I've got a few gettext() questions that are not covered anywhere in WordPress Codex but I think they are important. __('string') in THEME should be used for strings that are likely to be present ...
6
votes
2answers
992 views

Extract image from content and set it as the featured image

The problem: I'm posting from the Wordpress iPhone app. When adding an image, the image shows up in the description itself, which I don't like. What I'm trying to do: Remove the image from the ...
5
votes
5answers
2k views

Is it possible to rename a post format?

Since WP 3.1, it's been possible to use Tumblr-style post formats. I want to use the 'aside' option in a theme, but I want it to have a different title in the WP admin area. So, for example, when a ...
5
votes
5answers
983 views

Is there any global functions.php file which works for any theme?

Is there some global kind of functions.php file that works for any theme? The problem is here: When I change a functions.php file in any theme, I need to take care of two changes: First, I need to ...
5
votes
2answers
7k views

Disable plugin / plugin action via theme

I have installed the Google Analyticator plugin on my site. I am also using the MobilePress plugin to serve up a theme designed for non-smart-phone mobile browsers. I have set up Google Analytics for ...
5
votes
3answers
2k views

How to use a custom post type archive as front page?

I'd like to use a custom post type archive as a site's front page, so that http://the_site.com/ is a custom post type archive displayed according to my archive-{post-type}.php file. Ideally I ...
5
votes
1answer
426 views

Why does WordPress add 0 (zero) to an Ajax response?

Unless I end Ajax processing function by exit() or die(), the Ajax function receives desired and correct output but following by 0. Any idea why is that happening? Is that meant to be like that or ...
5
votes
3answers
3k views

Why use if function_exists?

I noticed a lot of developers of WordPress themes would use this in functions.php if (function_exists('register_nav_menus')) { register_nav_menus (array('primary' => 'Header Navigation')); } ...
5
votes
3answers
297 views

Difference between the_permalink() and get_permalink() function

In WordPress I am using both the_permalink() and get_permalink() functions, but I cannot get any difference in the output of both functions. What is the difference between both functions?
5
votes
4answers
2k views

Is it possible to remove the “standard” post format?

I'm using post formats heavily on a project, but have no current need for the standard option. Beyond being an unnecessary bit of UI, it actually causes broken behavior if it does get mistakenly ...
5
votes
3answers
2k views

Change admin bar to default:off

While I quite like the admin bar I actually want it to be OFF by default instead of ON ( I don't want to disable it altogether because I want users to be able to turn it on if they want - but but I ...
5
votes
4answers
2k views

Does Wordpress have a “Form API”

I do most of my development in Drupal, however I am working on a Wordpress site and I need to make a form. Which got me thinking, is there a Form API for wordpress like there is Drupal? Is there a way ...
5
votes
2answers
759 views

Use AJAX in shortcode

I have the following code to the shortcode to display a random quote. Question: how to make a button display a new random quote? I mean, that would hit the button and show you a new quote (without ...
5
votes
2answers
1k views

Modify Admin Bar Link

Starting on line 474 of /wp-includes/admin-bar.php there is a function that is declared as the following: function wp_admin_bar_new_content_menu( $wp_admin_bar ) What it does it generates the + New ...
5
votes
2answers
163 views

username_exists() function can't be access without logging in

I'm using the following function in functions.php to check if a username exists in the DB function check_username() { $username = $_POST['user']; if ( username_exists( $username ) ) { ...
5
votes
4answers
2k views

Removing Metabox for “Slug” without removing functionality

Here is an interesting question. I have recently noticed that if you utilize the code remove_meta_box('slugdiv', 'post', 'normal'); you are actually unable to modify the slug when you click on ...