Articles of filters

Should I use add_action('publish_post or add_filter('publish_post?

I am trying to trigger an extra function as soon as a certain custom post type is published function insert_table_products($post_id, $post) { if ($post->post_type == ‘custom-products’) { global $wpdb; $custom_meta = get_post_meta($post_id); //print_r($custom_meta); //print_r($post); $attachments = new Attachments(‘my_attachments’); if ($attachments->exist()) : $my_index = 0; $use_image = new SplFileInfo($attachments->url($my_index)); $use_main_image = $use_image->getFilename(); endif; $wpdb->insert( ‘products’, array( […]

Add a Span Around a Product Title in WooCommerce

I’m customizing a custom theme to use WooCommerce. One of the issues I’ve run into is adding a span tag around a product title on the single product page. I’ve figured out how to add a filter to the the_title method but unfortunately that method is used in other places like in the product image […]

How to add_filter/action to comment out CSS generated by admin function?

So in wp-includes/admin-bar.php there is a function that adds CSS styling to the page: function _admin_bar_bump_cb() { ?> <style type=”text/css” media=”screen”> html { margin-top: 28px !important; } * html body { margin-top: 28px !important; } </style> <?php } I want to comment this out properly (either the function or the contents of the function, either […]

Modify site URL via functions.php of a theme

My site URL in the database shows Is it possible to change this via functions.php into, however still maintaining in the database? My current scenario is that I have three developers who work locally and we want to use one database. We all connect to a remote database, however, the URLs from […]

How to apply content filter permanently?

I have the following temporary filter to fix a content error. We have data in our posts that have 4 digit zip codes, because the leading zero was cut when the original file was created. (For instance, a Massachusetts zip code is 0XXXX, but now in our content it is XXXX) function fix_zip_code($content){ if (preg_match(‘/<span […]

How do I filter title and alt attributes in the gallery shortcode?

I am displaying a post gallery inside a custom post type, using the following in my single-custom.php template: <?php echo do_shortcode(”); ?> One of the things included in the output is title=”filename” in the <img> tag, like so: <img width=”150″ height=”150″ src=”×150.jpg” class=”attachment-thumbnail” alt=”DSC_0036″ title=”DSC_0036″ /> I do not want to output the title attribute. […]

Adding body class when post contains a specific shortcode

Simple shortcode function : function my_shortcode_function($atts,$content=null){ // Do something return $content; } add_shortcode(‘my_shortcode’,’my_shortcode_function’); I wonder how to add more extra body class when user use this shortcode in contents. I can’t do something like : function my_shortcode_function($atts,$content=null){ // Do something add_filter(‘body_class’,’my_body_class’); return $content; } function my_body_class($classes) { $classes[] = ‘foo’; return $classes; } add_shortcode(‘my_shortcode’,’my_shortcode_function’); Any […]

How to replace the_post_thumbnail template tag and show the first inside the post image instead

I want WordPress to return the first post image or a default image if no featured image has been set. My theme uses the_post_thumbnail many times, so I don’t want to go through and change all references to a new function. I’d rather filter the core. Here’s what I’ve added to functions.php: add_filter(‘post_thumbnail_html’, ‘my_thumbnail_html’, 10, […]

Wrap First Character after </h3><p> in a <span> tag

This is a continuation from a question I asked earlier about doing this with output from the_excerpt(). What I am trying to do is take the following output from the_content() to take the following output as it currently is: <h3>Heading</h3> <p>Paragraph</p> And have it do the following: <h3>Heading</h3> <p><span class=”dropcap”>P</span>aragraph</p> I successfully did this with […]

Failed to invoke other hook from the init hook

Related to this question (Use wp init hook to call other hooks?) but not the same. Sometimes, I found that the hook will failed to run when I place inside the init hook, e.g. Not Work: add_action(‘init’,’my_init’); function my_init() { add_filter(‘locale’, … } Work: add_action(‘init’,’my_init’); add_filter(‘locale’, … Of course some hooks/filters will just work okay […]