Tag Info

Hot answers tagged

33

The plugins are loaded right before theme (yes, I've been looking for excuse to use this): However it is wrong to think about either as point of code execution. For most cases everything should be hooked and executed no earlier than init hook. According to Codex widget registration with register_widget() should be hooked to widget_init. Because of that ...


25

Here's the process to change the labels (I changed posts to "contacts" in my example) function change_post_menu_label() { global $menu; global $submenu; $menu[5][0] = 'Contacts'; $submenu['edit.php'][5][0] = 'Contacts'; $submenu['edit.php'][10][0] = 'Add Contacts'; $submenu['edit.php'][15][0] = 'Status'; // Change name for categories ...


17

You should run the code after theme setup. function osu_twentyten_continue_reading_link() { return ' <a href="'. get_permalink() . '">' . __( 'Read on <span class="meta-nav">&rarr;</span>', 'twentyten-child' ) . '</a>'; } function osu_twentyten_auto_excerpt_more( $more ) { return ' &hellip;' . ...


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

When WordPress activates a plugin, it calls the activate_plugin() function. This function activates the plugin in a sandbox and redirects somewhere else on success. It's been used by a few authors to programmatically activate plugin dependencies. There's another function, deactivate_plugin(), that does a similar thing in reverse ... it's actually how ...


8

WordPress hooks work like Hollywood: you don't call them, they call you. But unlike Hollywood, they keep calling everyone on the list. It's normal for an action or a filter to have multiple functions hooked to it, from different plugins, or even just different functions in the WordPress core that all do something specific. It is not only possible, but even ...


8

Yes, and no! The way users work in a multisite scenario is that there is a single pool of users, who are then associated with blogs. So, logged in status is common across all the blogs, but you can use is_blog_user($blog_id) to determine whether the current logged in user is a member of that blog. An example: if (is_user_logged_in()) { if ...


8

Not exactly sure what you are trying to accomplish, but it looks like you are trying to prepend something to the beginning of the_content. Try this: add_filter('the_content', 'se24265_my_function'); function se24265_my_function( $content ) { $write = 'hello world'; $new_content = $write . $content; return $new_content; } Most filters will ...


8

add_action("user_register", "set_user_admin_bar_false_by_default", 10, 1); function set_user_admin_bar_false_by_default($user_id) { update_user_meta( $user_id, 'show_admin_bar_front', 'false' ); update_user_meta( $user_id, 'show_admin_bar_admin', 'false' ); } Place in theme functions file or you can make into a plugin. Once user registers it will ...


8

The difference between theme and non-theme code is organizational rather than technical. Any code that is active contributes to resulting environment, does not matter where it is loaded from. There is number of places where code gets loaded from, which are not part of WordPress core: wp-config.php configuration file active theme (and its parent in for ...


8

after u have set a static page as your home page you can ad this to your functions.php and you a good to go. This will call the archive-POSTTYPE.php template correctly as well. add_action("pre_get_posts", "custom_front_page"); function custom_front_page($wp_query){ if($wp_query->get('page_id') == get_option('page_on_front')): ...


8

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

While you can specify separators and such in the_terms() arguments, it assumes that you actually want links. You can discard unwanted HTML by using filter: add_filter('the_terms', 'no_terms_links', 10, 2); function no_terms_links($term_list, $taxonomy) { if ('type' == $taxonomy) return wp_filter_nohtml_kses($term_list); return ...


7

First of all, make sure you try things out in a development environment on your own PC. This way you can try things out without breaking your live site. The XAMPP package contains everything you need to run it on your own Windows, Mac, Linux or Solaris machine. Then, enable debugging mode by setting WP_DEBUG to true in your wp-config.php. You need this to ...


7

There is a global variable in wp-admin called $pagenow which holds name of the current page, ie edit.php, post.php, etc. You can also check the $_GET request to narrow your location down further, for example: global $pagenow; if (( $pagenow == 'post.php' ) && ($_GET['post_type'] == 'page')) { // editing a page } if ($pagenow == ...


7

Coming in late to this party, but here's the "WordPress" way: use plugin_dir_path( __FILE__ ), e.g.: <?php include( plugin_dir_path( __FILE__ ) . 'ipn/paypal-ipn.php'); ?> Note that the function does return the trailing slash for the filepath.


7

Hi @NetConstructor: Another easy one! :) As you know I think very highly of WordPress in general, but the admin menu architecture is one of the nastiest hacks I think I've ever seen in open-source software. What's only greater in magnitude is the core WordPress team's unwillingness to acknowledge how bad it is and their lack of willingness to discuss ...


7

Well it wouldn't always work unless you global $user_id. is_user_logged_in will however work without that extra line of code. The speed improvement is most likely so small it's less than the speed improvement between single and double quotes and not even worth thinking about. Also $user_id variable may disappear in a new version and would promptly break ...


7

There is, as far as I know, no way to hook into wp-config.php from a Theme. For one, wp-config.php shouldn't be writeable; for another, wp-config.php is executed well-before a Theme's functions.php file is parsed. For a great overview of how WordPress boots, have a look at this two-part post by Theme.FM (part 1, part 2) or this Explanation with a flowchart ...


7

Try this instead: function settings_sections() { // array containing settings identifiers $array = ( 'SOMETHING', 'SOMETHING2', 'SOMETHING3', 'SOMETHING4' ); // loop through settings identifiers and generate settings sections foreach( $array as $v) { add_settings_section( $v . '_settings', $v . ' Settings', ...


7

See badlearner's edit below You can remove the default shortcode and create your own. Like so (in your functions.php): remove_shortcode( 'gallery' ); function my_own_gallary() { // Gallery code } add_shortcode( 'gallery' , 'my_own_gallary' ); The easiest way to alter the shortcode is to copy paste it in your functions.php and change the function to ...


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

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

Child themes reference parent themes by directory name, and in a normal install all your themes live in wp-content/themes/, so I'd say it's fine to reference those themes by their relative path: include '../parent-theme/some-file.php'; If that makes you uncomfortable, I observe the following constants in WordPress 3.0.1 with a twentyten child theme called ...


6

Here is what I came up with, seems to work: add_filter( 'pre_post_tags_input', 'no_tags_input_create' ); add_filter( 'pre_post_tax_input', 'no_tax_input_create' ); function no_tags_input_create($tags_input) { $output = array(); foreach( $tags_input as $tag ) if( term_exists( $tag, 'post_tag') ) $output[] = $tag; return ...



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