Posts

Showing posts with the label wordpress

To use WordPress's wp_delete_user function in your theme or plugin

Just add this line before calling wp_delete_user function: require_once(ABSPATH.'wp-admin/includes/user.php'); wp_delete_user($user->ID);

Medium to Wordpress migration

Image
Even though the writing and reading experience on the  Medium platform is very great,  I had to move my publication to WordPress because Medium's lack of customization. The following steps showing how I did that: 1. Make sure you expose your medium articles to RSS feeds in full text format: Go to "Homepage and settings" >> Syndication >> Select " Full " >> Click " Save " 2. In your wordpress site, install the WP PIPES plugin, activate it: https://wordpress.org/plugins/wp-pipes/ 3. Then go to the Pipes sidebar menu and set it up: Configuring source (RSSReader, https://your.medium.pulication.url/feed) and destination (post): Mapping fields from feeds and wordpress posts: 4. Click " Test this pipe " and enjoy Note: I haven't found a way to migrate the images to WordPress, so I have to do it manually for now.

Arrange posts by "order" attribute in WordPress

It's just simple as this function: function my_get_posts( $query ) {     if ( is_home() && false == $query->query_vars['suppress_filters'] )                 $query->set('orderby', 'menu_order');                 $query->set('order', 'ASC');      return $query; } Basically, you need to order the posts by menu_order attribute.

Mass change timezone of all sites in WordPress Multisite

I would use the  wp-cli  command line tool to reset timezone of all sites in my WordPress Multisite. For example to GMT+7 wp site list --field=url --allow-root | xargs -n1 -I {} sh -c 'wp --url={} option update gmt_offset 7 --allow-root'

WordPress Multisite - Create user and blog in command line using php

If you have a WordPress Multisite instance and you have to create blogs for your users, using the web interface of WordPress is not a good idea because you have to do it one by one. Here is a solution: 1. Create a csv file contains all the user information. 2. Using php and WordPress's function to create users and blogs. 3. Enjoy You can start from here. This is a php script I wrote to create a single user, blog, and add that user as an administrator of the blog: Usage: 1. Place that script inside a subfolder of your WordPress installation, for example: /var/www/wordpress/your_custom_scripts/create_user_blog.php 2. Run it: $ php /var/www/wordpress/your_custom_scripts/create_user_blog.php <username> <password> <user's display name> <user's blog path> <site category> Notes: 1. In the script I used my company-assigned id_number as user's blog path. 2. I also add site_category option to the new blog to categorize all my ...

WordPress - Bypass W3 Total Cache when running php script in command line

If your wordpress site's using W3 Total Cache, there is a big chance that your php script will stop working because of the cache, especially db cache. Here is my solution for that: Flush W3 Total Cache cache before running any functions. You simply put this line of code before executing any wordpress's function: if (function_exists('w3tc_dbcache_flush')) { w3tc_dbcache_flush(); } For example: ...                 // Flush W3TC DB cache before creating blog                 if (function_exists('w3tc_dbcache_flush')) { w3tc_dbcache_flush(); }                 $blog_id = wpmu_create_blog('my.multisite.blog', $path, $title, $user_id, $meta); ...

Get activated theme on every site in WordPress Multisite using Python

This is a more precise way to get the list of activated theme on each blog in your WordPress Multisite:

Get activated theme on every site in WordPress Multisite using wp-cli

You can use this: 1. Install wp-cli . 2. Create a bash script (export_blog_theme.sh) with the following content #! /bin/bash for url in $(wp site list --field=url --path=/path/to/your/wordpress/root) do         theme=$(wp option get template --url="$url" --path=/path/to/your/wordpress/root)         echo "$url,$theme" done 3. Make script executable: $ sudo chmod +x export_blog_theme.sh 4. Run the script: $ sudo -u www-data ./export_blog_theme.sh > blog_theme_list.csv Notes: Sometimes, the script return no result because of caches or WordPress db connection limitation.

Disabling spam email notification of AntispamBee plugin in WordPress multisite

Antispam Bee is a great spam filtering plugin for WordPress. It's free of charge and ad-free. But the thing is when you apply it for your network, a WordPress multisite with hundred or thousand of blogs, you have to set it up one-by-one. Luckily, I found an easy way to apply the default settings of Antispam Bee for all the blogs, such as disable the spam email notifications or even delete the spam without sending it to the spam folder. It's quite easy, you just need to open the  antispam_bee.php  (/path/to/your/wordpress/wp-content/plugins/antispam-bee/) file and modify these lines as following (around line #370, #371):                                 'flag_spam'             => 0,                                 'email_notify'          => 0,

Using SMTP for emailing in WordPress Multisite

To enable SMTP emailing (use SMTP instead of mail()) in a WordPress Multisite, you can do this: 1. Install this plugin in network admin dashboard: WP MAIL SMTP ( https://wordpress.org/plugins/wp-mail-smtp/ ) 2. Add these settings to  wp-config.php : define('WPMS_ON', true); define('WPMS_MAIL_FROM', 'myemail@mydomain.com'); define('WPMS_MAIL_FROM_NAME', 'My WordPress Multisite'); define('WPMS_MAILER', 'smtp'); // Possible values 'smtp', 'mail', or 'sendmail'  define('WPMS_SET_RETURN_PATH', 'false'); // Sets $phpmailer->Sender if true define('WPMS_SMTP_HOST', 'smtp.gmail.com'); // The SMTP mail host define('WPMS_SMTP_PORT', 587); // The SMTP server port number define('WPMS_SSL', 'tls'); // Possible values '', 'ssl', 'tls' - note TLS is not STARTTLS define('WPMS_SMTP_AUTH', true); // True turns on SMTP ...

#600 Fix Wordpress's admin menu issue in Chrome 45

Image
If you noted, the admin menu of WordPress (4.3) is messed in Chrome (v45): According to a StackOverflow post there is a known bug in chrome (more info here ). The source of the problem stems from Slimming Paint which is enabled by default in Chrome 45. Disabling slimming paint fixes the issue. To disable this feature, visit chrome://flags/#disable-slimming-paint in Chrome and Enable the Disable slimming paint option, and make sure the other two Enable options are disabled because they will override the Disable option. Hopefully, it will be fixed in Chrome  version 47 . For now, you can do as following to solve this issue: add this in your template's functions.php (single site) or in mu-plugins/functions.php (multisite): function admin_menu_fix() {     echo '<style>     #adminmenu { transform: translateZ(0); }     </style>'; } add_action('admin_head', 'admin_menu_fix'); References: [0] http://stackoverflow.com/ques...

Mass update/import setting (option) in WordPress Multisite using WP-CLI

Ok, just put this shell script inside your wordpress's root: Then run it. For example, I want to update the tadv_settings (TinyMCE Advanced) to all the blogs in my network: /my/wordpress/root/$ ./update_setting.sh tadv_settings /path/to/my/settings.txt The setting file look like this, settings.txt: {"toolbar_1":"fontselect,fontsizeselect,forecolor,backcolor,bold,italic,underline,blockquote,wp_code,bullist,numlist,alignleft,aligncenter,alignright,alignjustify,link,unlink","toolbar_2":"formatselect,undo,redo,table,media,image,hr,subscript,superscript,strikethrough,outdent,indent,pastetext,removeformat,charmap,wp_more,emoticons,code,fullscreen","toolbar_3":"","toolbar_4":"","options":"advlist,contextmenu,advlink,menubar,image","plugins":"anchor,code,insertdatetime,nonbreaking,print,searchreplace,table,visualblocks,visualchars,emoticons,advlist,link,contextmenu...

Delete all pending/spam/trash comments from WordPress (Single or Multisite) using WP-CLI

I just wrote this bash shell script to delete all pending/spam/trash comments from WordPress (Single or Multisite) using WP-CLI: Put the script inside your WordPress root and run: $ ./clean_comments.sh These are available statuses of a comment: approve : approved comments hold : pending comments spam : spams trash : comments in the trash bin This is pretty neat huh?!

WordPress - How to migrate media (attachment, gallery) properly (single or multisite)

One of the big problem when you migrate your WordPress blog to another server with a new domain name is the media gallery. What happened to me was even though I had already moved all the photos (media) to the new location but nothing showed up in the Media dashboard and all the gallery shortcode in posts were broken. I figured out earlier today that I need to update the media links or attachments attribute when I importing the old data (exported WXR file). Here is the step-by-step detail: (I'm doing this for my multisite WordPress) 1. In the old WordPress: Export old blog data (Tools > Export > select All Content > press Download Export File). You will have a xml file. 2. Copy over all the photos from the old WordPress to the new one. For example: Old: /var/www/oldblog/wp-content/blogs.dir/777/ New: /var/www/newblog/wp-content/uploads/sites/777/ 3. Open the export file using a text editor. Search and replace the old media path with the new one. For example: Ol...

Get WordPress Multisite blog_id by blog's path using wp-cli

You have a csv file has this format (mycsv.csv): path,some_field somepath,some_value anotherpath,another_value ... and you want to get the blog_ids of those path(s). This following bash shell will help you to get blog_ids by taking advantages of wp-cli, get_blogid_from_url.sh: $ chmod +x get_blogid_from_url.sh $ ./get_blogid_from_url.sh mycsv.csv myblog.com /wordpress/path > result.csv The result will be something like: 546,myblog.com/somepath/ 456,myblog.com/anotherpath/ ...

The best way (until now) to change domain name of a WordPress Multisite

Important Note: If you change the "DOMAIN_CURRENT_SITE" option in your wp-config.php of your multisite, you will not be able to connect to the database (via web and wp-cli) You should use this tool to change the domain name of your multisite: Github repository:  https://github.com/interconnectit/Search-Replace-DB 1. Clone the git repo into your WordPress root: $ git clone https://github.com/interconnectit/Search-Replace-DB 2. Open the browser, go to http://old.multisite.com/Search-Replace-DB/ and start searching and replacing: First search and replace your old url to the new one on all the tables (you can click " dry run " to check first, click " live run " to actually make changes): Old URL: http://old.multisite.com New URL: http://new.multisite.com Then, search and replace old domain to the new one one on all the main wordpress table (wp_sites, wp_blogs,...): Old domain: old.multisite.com New domain: new.multisite.com Af...

Wordpress multisite migration with wp-cli

There are many way to migrate a multisite WordPress to a new one including: database import... In my case: I created new blogs for all the users first (in the new multisite, the blog_ids will be different than in the old multisite). Export data of all blogs (wordpress-importer) from the old multisite. (wp-cli) Import data (only posts, comments) exported from the old blogs. (wp-cli) Finally, copy over the media, rename the directory names with the new blog_id. Search and replace all the media links in the database (wp-cli) Here are the details steps: 1. Export data of all the blogs from the old WordPress multisite and transfer to the new one: We need to use my modification of wp-cli ( click here ) to set name of the backup data the blog's path: blog1path.xml, blog2path.xml... This way we will know which file belongs to which blog and user in the new multisite. Assuming: My custom wp-cli source is at /home/myuser/wp-cli/ Wordpress multisite root: /var/www/ Backu...

How to set name of the wxr file when exporting blog data using wp-cli

By default (actually, you cannot change right now), when you export blog data usingWP-CLI, the wxr will be name as following: myblogname.wordpress.2015.xml 1. Clone the wp-cli project: $ git clone https://github.com/wp-cli/wp-cli.git 2. Install composer and install WP-CLI's dependencies: $ curl -sS https://getcomposer.org/installer | php $ sudo mv composer.phar /usr/local/bin/composer $ cd /path/to/wp-cli/src/ $ composer install 3. Modify the get_filename_template function in /path-to-wp-cli-src/php/commands/export.php. For example, I want to name the exporting file after the blog's path: blog-1-path.xml $ nano /path/to/wp-cli/src/php/commands/export.php ... private static function get_filename_template() { $export_file_name = get_bloginfo('url') . ".xml"; $export_file_name = str_replace("http://", "", $export_file_name); $export_file_name = str_replace("/", "", $export_file_name); if( i...

Cleaning up your WordPress Multisite instance with wp-cli

At some points in life, your WordPress Multisite will become too big and needs to be cleaned up, things like spam comments, revisions, transients... And of-course, you don't want to go blog by blog to clean up all those things. It may take days or weeks if you have > 1000 sites like mine. Here is your savior: WP-CLI WP-CLI is a command line tool which can help you manage your WordPress site flawlessly (:D). This blog post will show you how to use wp-cli to clean up your WordPress Multisite including: 1. Transients: Create a shell script inside your WordPress dir root: $ cd /path/to/wordpress/root $ sudo nano clean_transients.sh #!/bin/bash for url in $(wp site list --field=url --allow-root) do   echo $url #used for progress purposes   wp transient delete-all --url=$url --allow-root done $ sudo chmod +x clean_transients.sh $ sudo ./clean_transients.sh --allow-root 2. Spam comments: $ cd /path/to/wordpress/root $ sudo nano clean_comments.sh ...

Change background image randomly from a directory using Ajax in WordPress

A couple weeks ago, I'm working on a project that required me to develop a WordPress theme. One of the things that interested me was to create a block (html, in the front page) that change its background image randomly every time I reload the page. Here is how I did that: The main idea here is using Ajax to dynamically change background of a div tag to a random image loaded from a folder inside the theme's root every time I reload the home page. 1. functions.php add_action('init', 'enqueue_scripts_styles_init'); function enqueue_scripts_styles_init () { wp_enqueue_script( 'ajax-script', get_template_directory_uri().' /js/myscript.js ', array('jquery')); wp_localize_script( 'ajax-script', 'ajax_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) ); } add_action('wp_ajax_get_rand_img', 'get_rand_img'); add_action('wp_ajax_nopriv_get_rand_img', 'get_rand_...