Tag Info

Hot answers tagged

30

Hi @Sruly: You've pretty much answered your own question, but I'll elaborate a bit. Action Hooks Actions Hooks are intended for use when WordPress core or some plugin or theme is giving you the opportunity to insert your code at a certain point and do one or more of the following: Use echo to inject some HTML or other content into the response buffer, ...


29

There are two types of rewrite rules in WordPress: internal rules (stored in the database and parsed by WP::parse_request()), and external rules (stored in .htaccess and parsed by Apache). You can choose either way, depending on how much of WordPress you need in your called file. External Rules: The external rule is the easiest to set up and to follow. It ...


24

I might be misunderstanding you, but why not use wp_create_nav_menu()? E.g., this is what I do to create a custom BuddyPress menu when I detect BP as active: $menuname = $lblg_themename . ' BuddyPress Menu'; $bpmenulocation = 'lblgbpmenu'; // Does the menu exist already? $menu_exists = wp_get_nav_menu_object( $menuname ); // If it doesn't exist, let's ...


23

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


19

The API you offer in a plugin or a theme depends on the logic of that specific code. There is probably no guide that applies to all situations. I am a contributor for multiple plugins with APIs, and what I have learned so far is: Do not offer an API until you really know how people use your code. Release the first two or three versions without any API. ...


16

if you look at the do_settings_sections function more specifically the line 1164 where the callback function is being executed : call_user_func($section['callback'], $section); you can see that the $section array is being passed to the callback function, so you can identify the callback by the $section['id'] hope this make since. Update here is an ...


15

WordPress expects the $_SERVER variables to be setup as if it were a normal web request. Also, I would suggest loading wp-load.php instead of wp-blog-header.php since you probably don't need the WP class or the template loader to run. Here is how I normally start any scripts I need to interact with WP from command line: define('DOING_AJAX', true); ...


13

I think the best option is an endpoint. You get all the data as a simple string, so you can decide how it will be parsed, and you don’t have to worry about collisions with other rewrite rules. One thing I learned about endpoints: keep the main work as abstract as possible, fix the glitches in WordPress’ API in a data agnostic way. I would separate the ...


9

There's a few bits here that apply, but the short of it is this code in customize-preview.js: this.body.on( 'click.preview', 'a', function( event ) { event.preventDefault(); self.send( 'scroll', 0 ); self.send( 'url', $(this).prop('href') ); }); The event.preventDefault prevents the links from actually working. The following code then sends a ...


9

The way to do this is to use the 'admin_enqueue_scripts' hook to en-queue the files you need. This hook will get passed a $hook_suffix that relates to the current page that is loaded: function my_admin_enqueue($hook_suffix) { if($hook_suffix == 'appearance_page_theme-options') { wp_enqueue_script('my-theme-settings', ...


8

This worked for me. I never ever touch the rewrite API, but am always up to push myself in new directions. The following worked on my test server for 3.0 located in a sub folder of localhost. I don't for see any issue if WordPress is installed in web root. Just drop this code in a plugin and upload the file named "taco-kittens.php" directly in the plugin ...


8

Cross-site Scripting Issues You cannot transfer WP auth cookies between domains. You also don't want to store plaintext passwords for logging into another WP installation programmatically. So, you'll have to have users log into WordPress, and then access their login status via an API endpoint from the third-party site. This lets WordPress handle all the ...


7

If you are using PHP 5.2+ you're best bet is to just make a PHP array or object and use json_encode(). UPDATED: $cats = get_categories(); $output = array('categories' => array()); foreach ($cats as $cat) { $cat_output = array( 'cat_id' => $cat->term_id, 'cat_name' => $cat->name, 'posts' => array(), ); ...


7

I tried for a bit and could not get this to work perfectly, but it's close, it's hard to extend with the <form> for the popup styles being hardcoded, thought possible with more work. To get started you can: Enqueue the link popup javascript and styles, the main .js file is wp-includes/wplink.js. Depending on where you loading this you might need to ...


7

The most comprehensive method is get_current_screen added in WordPress 3.1 $screen = get_current_screen(); returns WP_Screen Object ( [action] => [base] => post [id] => post [is_network] => [is_user] => [parent_base] => edit [parent_file] => edit.php [post_type] => post [taxonomy] => )


7

This question is going to get some opinion based answers, but well, I'll give it a try. There are a couple of reasons why someone might've told you that, it would be great to know the context also. Upto version 3.4 or 3.5, that function only worked when the parameter was a var, that might be one of the reasons why someone told you that. I, myself, don't ...


7

For starters let's dive into what is 5 really. It is the post's ID. But what is ID in turn? It is value in the MySQL table row which identifies the specific post record. Issues with using IDs So first there are some conceptual problems with it. It's not content. It's not something user creates, manipulates, or (most of the time) aware of. WP is (by design) ...


6

TL;DR There is no JavaScript API in the WordPress core and no one is planned, but actually, there is no need of it. Backend First of all let's say that, regarding the backend, some useful information can be fetched from already present JavaScript global variables (WordPress loves all global flavors). E.g. ajaxurl for the admin-ajax.php url to be used ...


6

Both approaches are not mutually exclusive. As @gmazzap said, don’t create a callback hell. But you can provide an initial hook, so other developers don’t have to rely on the rather slow function_exists() checks. Example In your plugin, provide a hook that other developers can use to call your classes and functions safely. add_action( 'wp_loaded', [new ...


6

First off: This Theme is so doing it wrong. One should not simply stuff plain calls in functions.php files. Those should be wrapped and hook. Best to after_setup_theme(). You could btw try the same hook.


6

wp_get_active_and_valid_plugins() get_plugins() and get_option('active_plugins')


6

There multiple WordPress Apis for getting a list of plugins like OTP listed in his answer. get_plugins() :Check the plugins directory and retrieve all plugin files with plugin data. Returns an array. Note: Only works on the admin side Example: function list_the_plugins() { $plugins = get_plugins(); foreach ( $plugins as $plugin ) { ...


6

As a complement to ZaMoose's anwser, here's how you would create a "Page-type" menu item (not a "Custom" one): wp_update_nav_menu_item($menu_id, 0, array('menu-item-title' => 'About', 'menu-item-object' => 'page', 'menu-item-object-id' => ...


6

To offer an alternative method/approach to the above question. // When you are viewing the users list or your editing another user's profile add_action( 'admin_print_scripts-users.php', 'your_enqueue_callback' ); // When you are editing your own profile add_action( 'admin_print_scripts-profile.php', 'your_enqueue_callback' ); function ...


5

I'm a little confused by your question, can you not simply use wp_enqueue_style, which you actually wrote into this threads heading(not sure if you know the function exists or not). wp_enqueue_style works in the same way as the wp_enqueue_script counterpart, but of course as you'd expect, enqueues stylesheets.. Here's a list of the enqueues i was able to ...


5

Let me apologize for the initial question I had about your motives. I see a lot of "how can I remotely retrieve all posts from another blog" questions and immediately assume there is nefarious intent because, 9 times out of 10, there is. That said, your purposes seem very straight-forward and respectable. Currently, there is no way to "chunk" the XML ...


5

WordPress features a very rich XML-RPC interface that you can work with from external applications. It provides you access to most of the functionality you'd have directly in the admin - write posts, edit posts, edit comments, create/edit categories, manage site options, upload files, etc. As a matter of fact, certain third-party applications are already ...


5

Hmmm, can't remember or find fitting function either. There is wp_set_object_terms() that is used in multiple wrappers like wp_set_post_categories(). It can overwrite categories for a post. So you can get post categories, check for unwanted one and write it back excluding unwanted in that case.


5

I know it's been a while, but if anyone else stumbles on this, I found a WordPress hook specifically for modifying HTTP headers. The hook is wp_headers and it's called in the wp class. The first argument passed is an array of headers with the header name as the key. The second argument is a reference to the wp class object.


5

I came up with a function that does the job for now : /** * Creates a translation of a post (to be used with WPML) * * @param int $post_id The ID of the post to be translated. * @param string $post_type The post type of the post to be transaled (ie. 'post', 'page', 'custom type', etc.). * @param string $lang The language of the translated post (ie ...



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