How to iterate through custom posts and add the title to an array

I see two problems with the code you’ve written. The first one is that the_title() echos the post title although returning the title would be more appropriate in this context. Also wp_reset_postdata() should be after the while loop inside the if statement. But as you mentioned that you only need the post titles, you can … Read more

Plugin Architecture/Design Pattern – is better to use a private Observer/Mediator Pattern for plugin subclasses or WP add_action?

I’m wondering if i should refactor my plugin using a ‘private’ Observer/Mediator pattern ie. collect all relevant add_actions to my parent class only and baking up a pattern to notify/forward subclasses of events, reducing the impact of my plugin to WP event ques. The event queue is fundamental to WP, so it’s pretty fast and … Read more

wp_localize_script $handle

It’s basically a unique id of the script you registered or enqueued before. Let’s say we enqueued a two scripts with wp_enqueue_script(): wp_enqueue_script( ‘my_script_1′,’/js/some_script.js’ ); wp_enqueue_script( ‘my_script_2′,’/js/some_other_script.js’ ); Now you want to pass your $data to the script #2 with wp_localize_script(): wp_localize_script( ‘my_script_2’, ‘my_script_2_local’, $data ); After this – when WordPress prints out your my_script_2 … Read more

Using AJAX in a plugin to submit form – REALLY confused

Things you can check: View the page source, and look for your js files, if they’re included or not, and whether their order is proper. Use console.log(SwoonAjax.ajaxurl) to check if you’re getting the proper path in your js file. In Firebug, switch to Console tab, and then try firing your AJAX request. You’ll see the … Read more

Show a user their recently viewed posts

You can add a timestamp to your post meta each time a product is viewed, then query the five most recently viewed products. Assuming you are using a custom post type named ‘product’, add the following inside the loop of your single-product.php template file: <?php if (get_post_type( $post->ID ) == ‘product’ ) update_post_meta( $post->ID, ‘_last_viewed’, … Read more

Plugin development with unit tests

The unit tests transform all CREATE TABLE and DROP TABLE queries to CREATE TEMPORARY TABLE and DROP TEMPORARY TALBE, respectively. So in your tearDown the query will attempt to drop temporary tables with those names, but not the actual tables. To fix this, add this before your DROP queries: remove_filter( ‘query’, array( $this, ‘_drop_temporary_tables’ ) … Read more

What is the added “complexity” of custom tables?

Custom tables can simplify your code, improve performance and help avoiding conflicts with WordPress’ schema changes in the future. Don’t hesitate to use one if you need it. The extra complexity comes from two other factors: maintenance and multisite. Maintenance: manual or automatic backups might omit custom tables, so there is a tiny risk that … Read more