Back to Top

Category: Wordpress

Bulk remove a category from posts

Almost unbelievably in a default wordpress install there is no way to bulk remove a category from posts via the dashboard, you can add them in the bulk edit mode, but not batch remove them from posts.

 

Only way I have found to be able to do it is as follows..

 

(1) Find the ID of the category you wish to remove posts from eg in this example category Diet has an id of 28281 – found by hovering over the edit tag and noting the tag_ID bit of the urlĀ  bit ?taxonomy=category&tag_ID=28281&post_type=post&wp_http_referer=%2Fwp-admin%2Fedit-tags.php%3Ftaxonomy%3Dcategory

 

 

(2) Add the following code to you themes functions file replacing {category_id} with the category id (numeric) found above in part 1 eg 28281

 

add_action( 'init', function()
{
    
    $args = [
        'posts_per_page' => -1, 
        'cat'            => {category_id}, // Category ID for category you wish to remove posts from 
        'fields'         => 'ids', 
            ];
    $found_posts = get_posts( $args );

    if ( !$found_posts ) return;

    foreach ( $found_posts as $id )
        wp_remove_object_terms(
            $id, // Post ID
            {category_id}, 
            'category' 
        );
}, PHP_INT_MAX );

3) Then load the page view to make sure the above code is triggered, then go back to dashboard and check that all posts have been removed from your selected category.

4) Finally when happy all posts have been removed from the category, re-edit your themes functions file, and remove the code you added in part 2 above.

5) Optional – delete empty category, if you wish.

Posted in Wordpress |

Setting The Path To Home Directory In PHP

Got two methods, one for WordPress and another for other general PHP scripts.

 

WordPress; Simply use the constant ABSPATH eg file(ABSPATH.”some-other-folder/some-file.txt”); – it returns the path to where wp-config.php is located for the current wordpress install.

 

General: Can use the following to obtain the home or root folder.. $doc_root = preg_replace(“!${_SERVER[‘SCRIPT_NAME’]}$!”, ”, $_SERVER[‘SCRIPT_FILENAME’]);

 

Note: $doc_root won’t include an end / – so on any additional file/path one will need to be added.

 

Posted in PHP, Wordpress |

How to Safely Enable SVG File uploading in wordpress

This code snippet for wordpress allows the importing of .svg files which by default are restricted on security grounds.. would only use if you know you are using sanitized .svg files and/or a single admin user of your website.

function cc_mime_types($mimes) {
 $mimes['svg'] = 'image/svg+xml';
 return $mimes;
}
add_filter('upload_mimes', 'cc_mime_types');

should be added to the bottom of theme functions.php file.

Posted in Wordpress |

mysql code to find wordpress posts not associated with a category

WordPress mysql code to find posts not associated with a category.

SELECT  * 
FROM    wp_posts p
WHERE   p.post_type = 'post' 
        AND p.post_status = 'publish' 
        AND NOT EXISTS
        (
        SELECT  *
        FROM    wp_term_relationships rel
        JOIN    wp_term_taxonomy tax
        ON      tax.term_taxonomy_id = rel.term_taxonomy_id 
                AND tax.taxonomy = 'category' 
        JOIN    wp_terms term
        ON      term.term_id = tax.term_id
        WHERE   p.ID = rel.object_id 
        )
Posted in Wordpress |

Replacing youtube object embeds

Since google/ youtube pulled object/embed support for embedded youtube videos, here is a wordpress filter for attempting to replace object embeds with video specifiers for use in other plugins (code could be adjusted for direct iframes too), it checks to see if an iframe or youtube reference already exists as it builds up an array of youtube urls – only uses first youtube url found. It assumes any youtube urls are in . Maybe of use to somebody.

function replaceyoutubeobject($string) {
    if(stripos($string,'iframe')===false) {
    if(stripos($string,'+<\/object>/smi", "", $string); // remove old objects
    $match = $matches[0];
    $url = $match[0];
    if(stripos($url,"/v/")!==false) {
    $u=explode("/v/",$url);
    } else if(stripos($url,"v=")!==false) {
    $u=explode("v=",$url);
    }
    $u1=$u[0];
    $u2=$u[1];
    $ext=explode("&",$u2);
    $vcode=$ext[0];
    $string = "
\n".$string; }}} return $string; } add_filter( 'the_content', 'replaceyoutubeobject' );
Posted in Wordpress |

Fixing wordpress admin ‘white/blank dashboards’

If you log into admin dashboard via wordpress and are faced with blank/white dashboard areas, try the following fix.

Open file blog/wp-admin/includes/screen.php in your favorite text editor.
On line 706 or close by (depending on version) find the following PHP statement: echo self::$this->_help_sidebar;
Replace it with the statement: echo $this->_help_sidebar;
Save your changes.
Go to a dashboard page.
Posted in Wordpress |

Redirecting yahoo emails for contract form 7 plugin

Sometimes yahoo blocks server smtp email checks, so emails aren’t sent out to none yahoo addresses. This can be solved using the code below (tested for wordpress 3.9, and latest version of contact form 7 plugin):

if (!function_exists('cf7_redirect_yahoo')) {

function cf7_redirect_yahoo($cf7)
{

       $domain = get_option("siteurl");
       $domain = str_ireplace("http://","",$domain);
       $domain = str_ireplace("www.","",$domain);
       $fd=explode("/",$domain);
       $nmail="noreply@".reset($fd);
       $mail = $cf7->prop('mail');

       $submission = WPCF7_Submission::get_instance();
       if($submission) {
       $posted_data = $submission->get_posted_data();
       foreach ($posted_data as $keyval => $posted) {
       if(stripos($keyval,'email')!==false) {
       if(stripos($posted,'yahoo')!==false) {
       $mail['sender'] = $nmail;
       $cf7->set_properties( array( 'mail' => $mail ) );
       }}}}

       return $cf7;
}
}
Posted in Wordpress |

Fixing wpcf7_before_send_mail() hooks – Contact Form 7 Plugin

In wordpress Contact Form Plugin version 3.9 onwards the structure of the plugin changed, and this has had a major impact on the wpcf7_before_send_mail() hook usage. If you now want to access the email posted data – keys and post value, below shows the old method and then the new method. Hope it helps someone. Note that the posted_data object property no longer exists.

Old Method:

add_action("wpcf7_before_send_mail", "my_function");

function my_function(&$cf7)
{

foreach ($cf7->posted_data as $keyval => $posted) {
// use $keyval and $posted as elements in email forms
}

}

New Method:

add_action("wpcf7_before_send_mail", "my_function");

function my_function($cf7)
{
       $submission = WPCF7_Submission::get_instance();
       if($submission) {
       $posted_data = $submission->get_posted_data();
       foreach ($posted_data as $keyval => $posted) {
       // use $keyval and $posted as elements in email forms
       }
       }
}
Posted in Wordpress |