• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

My Monkey Do

A Log of Coding Solutions

  • Home
  • Web Hosts
  • Tools
  • About

wordpress

WordPress – Change Post Counts on edit.php

October 9, 2012 by Webhead

Problem

I wanted to change what posts were displayed to a user when they logged into wordpress and view the Posts page  I could easily alter what posts are shown by using the ‘pre_get_posts’ filter, but the problem was that the number of posts for each status did not change.  You know, at the top of the page it looks like: “All (5) | Published (5)”.  The number 5 did not reflect the number of posts being shown.

Solution

I was hoping to find a filter of some sort on the wp_count_posts function, but there are none.  The second best solution is to rewrite that whole line using the ‘views_edit-post’ filter where “edit-post” is the page id.  A sample of what can be done is in the link below.

 

source:  http://wordpress.stackexchange.com/questions/36117/wp-post-list-tableget-views-have-post-counts-account-for-filters
keywords:  wp_count_posts filter

Filed Under: Coding Tagged With: php, wordpress

Caching With Widgets

September 24, 2012 by Webhead

W3 Total Cache is a great WordPress plugin that will help your Google Page Speed score and will also speed up your website.  The only problem is that it doesn’t clear the cache when widgets are updated.  So for example if you have a custom theme with a widget on your page and you update this widget, changes are not going to be seen.  You’ll have to empty the cache on the W3 Total Cache page.

WPEngine also has a caching system that doesn’t clear the cache upon editing widgets.  This means that every time you update a widget you have to manually clear the cache.  WPEngine is a great WordPress host, but doing something manually isn’t good for clients.

A solution is to add a function to clear the cache every time the widget is updated.  The code below covers W3 Total Cache, WP Super Cache and WPEngine.  You can copy-paste it into your functions.php or just install the Clear Cache For Widgets plugin.

function clear_cache_for_widgets( $instance ) {

// if W3 Total Cache is being used, clear the cache
if ( function_exists( 'w3tc_pgcache_flush' ) ) {
w3tc_pgcache_flush();
}
// if WP Super Cache is being used, clear the cache
else if ( function_exists( 'wp_cache_clean_cache' ) ) {
global $file_prefix;
wp_cache_clean_cache( $file_prefix );
}
else if ( class_exists( 'WpeCommon' ) ) {
WpeCommon::purge_memcached();
WpeCommon::clear_maxcdn_cache();
WpeCommon::purge_varnish_cache();
}

return $instance;
}
add_filter( 'widget_update_callback', 'clear_cache_for_widgets', 10 );

Source: http://scratch99.com/wordpress/development/clearing-cache-when-widget-saved/
http://wordpress.stackexchange.com/questions/25425/page-cache-for-categories-not-updating-with-w3-total-cache

Filed Under: Coding, Plugins Tagged With: google, optimize, php, wordpress, wpengine

WordPress Widget Image Field

September 19, 2012 by Webhead

I found a great utility plugin to help with adding a Image picker field to a widget.  Basically you install and activate this plugin and you make your own widget and utilize the image field easily.
The only problem I had was that it didn’t work in Firefox.  I found a simple error on line 72 in the scripts.js file.  Instead of calling events.target.href it should be e.target.href because e is the parameter passed in.

 

how to use: http://mondaybynoon.com/20120206/wordpress-widget-image-field/

Filed Under: Coding Tagged With: debug, javascript, wordpress

Custom Post Type Templates in Plugins

September 12, 2012 by Webhead

Problem

I made a plugin that created a custom post type, but the only way I could customize the single-customposttype.php and archive-customposttype.php is by placing the files into the theme.  It would be nice if everythere could be contained in the plugin and the theme could override if needed.

Solution

WordPress to the rescue… There is a filter called ‘template_include’ that can be used to set the template.  It’s a general filter so you’ll need to check for your post type.

add_filter( 'template_include', 'my_plugin_templates' );
function my_plugin_templates( $template ) {
    $post_types = array( 'project' );

    if ( is_post_type_archive( $post_types ) && ! file_exists( get_stylesheet_directory() . '/archive-project.php' ) )
        $template = 'path/to/list/template/in/plugin/folder.php';
    if ( is_singular( $post_types ) && ! file_exists( get_stylesheet_directory() . '/single-project.php' ) )
        $template = 'path/to/singular/template/in/plugin/folder.php';

    return $template;
}

source: http://wordpress.stackexchange.com/questions/55763/is-it-possible-to-define-a-template-for-a-custom-post-type-within-a-plugin-indep
keywords:  plugin template custom post type


Filed Under: Coding Tagged With: php, wordpress

wp_insert_post post_category problem

September 11, 2012 by Webhead

Problem

I was wp_insert_post to programmatically create a custom post type with a custom category.  Using the following code did not work however.

$new_post = array(
‘post_title’ => ‘some title’,
‘post_content’ => ‘some description’,
‘post_status’ => ‘publish’,
‘post_author’ => $admin_user->ID,
‘post_category’ => array($category->term_id)
);
wp_insert_post($new_post);

Solution

If I read the wordpress codex better and more thoroughly I probably would have seen that there is another special parameter for custom taxonomies.  Instead of using ‘post_category’ I should be using ‘tax_input’ for custom taxonomy categories.  So instead of the ‘post_category’ line, it would look like:

‘tax_input’ => array($category->taxonomy => array($category->cat_name ));

If this doesn’t work, like it didn’t for me because I’m using it in a CRON, the user you are using may not have rights to modify the taxonomy.  in that case, as the docs say, use wp_set_object_terms.  The problem is that wp_insert_post uses current_user_can to detect if the current user can add the taxonomy.  The issue is documented here: http://core.trac.wordpress.org/ticket/19373

 

source: http://wordpress.org/support/topic/problem-with-wp_insert_post-for-custom-post-type-category-is-not-associated
keywords:  wordpress post_category wp_insert_post

Filed Under: Coding Tagged With: php, wordpress

WordPress “The plugin generated xxxx characters of unexpected output during activation”

September 11, 2012 by Webhead

Problem

I was developing a plugin for wordpress and receive a messsage:

The plugin generated 496 characters of unexpected output during activation

Solution

I placed the following code at the end of my plugin file,

update_option(‘plugin_error’,  ob_get_contents());

then looked in the wordpress options table using the query

“SELECT *
FROM `wp_options`
WHERE option_name = ‘plugin_error'”

Then I could see what the unexpected output was.

Or I could’ve installed the WH Debug plugin and just added this to the end of the plugin file:

wh_debug( ‘plugin_error’, ob_get_contents());

And then viewed it in the WordPress admin pages under Tools->WH Debug.

 

source: http://hungrycoder.xenexbd.com/wordpress-2/how-i-have-solved-the-the-plugin-generated-xxxx-characters-of-unexpected-output-during-activation-problem.html

keywords: wordpress characters of unexpected output during activation

 

Filed Under: Coding Tagged With: debug, mysql, php, wordpress

  • « Go to Previous Page
  • Go to page 1
  • Interim pages omitted …
  • Go to page 6
  • Go to page 7
  • Go to page 8
  • Go to page 9
  • Go to page 10
  • Interim pages omitted …
  • Go to page 12
  • Go to Next Page »

Primary Sidebar

Topics

apache apple bootstrap buddypress chrome cloudways cms css debug drupal eCommerce firebug firefox git gmail goDaddy google hosting htaccess html html 5 IE crap image iPad iPhone javascript jquery kinsta linux localization mac os x ms sql mysql open source optimize php php 7.2 svg tinymce woocommerce wordpress wpengine xss yii youtube




Categories

  • Coding
  • Off the Shelf
  • Plugins
  • Random Thoughts
  • Server Stuff
  • Tools
  • Uncategorized