Redirect page programmatically

This code adds an action to WordPress which redirects the user to the contact page when the about page is visited. The redirection is done using a 301 status, meaning it is a permanent redirect.

Deactivate plugin programmatically

This code checks if the specified plugin is active and, if so, deactivates it. This is useful for ensuring only one version of a plugin is active at a time.

Programmatically save post

This code creates a WordPress post with the values of the ‘post_title’ and ‘post_content’ variables. It sets the post status to ‘publish’ and the author to one. If the post is successfully inserted, it will execute the code inside the if statement.

Create order programmatically

This code creates a new post in the WordPress database with post type ‘shop_order’, post title ‘Order #12345’, post status ‘wc-processing’, and ping status ‘closed’. The post ID is stored in the $order_id variable.

Change site name programmatically

This code updates the ‘blogname’ option to the new value ‘New Site Name’ in the WordPress database. The goal is to update the site title/name to the new value.

Update post name programmatically

This code updates an existing post in the WordPress database with a new post title. It takes the post ID and a post name as parameters and updates the title accordingly.

Programmatically create nav menu

$locations ); } ?> This code creates a navigation menu with a single item, ‘Home’, and assigns it to the primary location. It first checks if the menu exists, and if not, creates it using the wp_create_nav_menu() function. It then adds the ‘Home’ menu item using wp_update_nav_menu_item(), and assigns it to the primary location using […]

Delete media programmatically

This code deletes all attachments from the WordPress database. It creates a new WP_Query object containing all posts of the post type ‘attachment’, then iterates through them, deleting each one. Finally, it resets the postdata to ensure no conflicts.

Set primary menu programmatically

This code creates a WordPress navigation menu with the name “Primary Menu”. If the menu already exists, it updates its locations in the database. Otherwise, it is created and its locations are updated in the database.

Add post meta programmatically

This code adds metadata to a post, taking the post ID, meta key, meta value, and a boolean value for whether the meta should be unique or not as parameters. The function assigns the passed meta value to the post, meaning the post can be identified by the metadata.