WordPress

How to Save Options to Database in WordPress
How to Save Options to Database in WordPress

One of the simplest ways to store data in WP is to use the database table called “wp_options”. Typically, this table is used to store plugin or theme data that is not related to meta values (such as for posts, users, or taxonomies).

WordPress provides a set of built-in functions and filters to interact with this table.

Let’s take a look at a small working example:

$option_key = 'my_option_var';

echo "<b>get_option</b>";
$result = get_option($option_key);
var_dump($result);
echo "<hr>";

echo "<b>get_option + default value</b>";
$result = get_option($option_key, 'default_value');
var_dump($result);
echo "<hr>";

echo "<b>add_option with string</b>";
$result = add_option($option_key, 'option_value', '', false);
var_dump($result);
echo "<hr>";

echo "<b>get_option</b>";
$result = get_option($option_key);
var_dump($result);
echo "<hr>";

echo "<b>update_option with array</b>";
$result = update_option($option_key, array(
	1 => 'One',
	'Two' => 3,
	'Three' => 'Three',
));
var_dump($result);
echo "<hr>";

echo "<b>get_option</b>";
$result = get_option($option_key);
var_dump($result);
echo "<hr>";

echo "<b>delete_option</b>
";
$result = delete_option($option_key);
var_dump($result);
echo "<hr>";
die;

read more...

How to Create a Table in WordPress Database
How to Create a Table in WordPress Database

Creating a custom table for the WordPress CMS may be necessary in the following cases:

  1. When developing your own plugin
  2. When creating your own theme (e.g., for real estate, car sales or rental, etc.)

In my projects, to create tables or import data into the database, I use the WP function `dbDelta` within the `register_activation_hook()`. Example code:

register_activation_hook(__FILE__, function()
{
	global $wpdb;
	
	require_once(ABSPATH.'wp-admin/includes/upgrade.php');

	dbDelta("CREATE TABLE IF NOT EXISTS `{$wpdb -> prefix}my_table` (
		`id` INT(11) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
		`title` VARCHAR(255) NOT NULL,
		`date_create` INT(10) UNSIGNED NOT NULL
	) {$wpdb -> get_charset_collate()};");	
});

read more...

How to disable date filter in posts or custom post types
How to disable date filter in posts or custom post types

Sometimes there are cases when, in custom post types (created via the `register_post_type()` function), or even in default post types like post or page, we need to disable (remove) the date filter dropdown. This dropdown shows the month and year when posts were created on our site.

Searching the official documentation didn't yield much, so I had to dig into the code and find the necessary filter. I discovered the `disable_months_dropdown` filter, which takes two parameters — the display state of the dropdown and the post type.

Example of using the filter:

add_filter('disable_months_dropdown', function($bool, $post_type) {
	if($post_type == 'type_order')
	{
		return true;
	}
	return $bool;
}, 1, 2);

where:
$bool — whether to display the date dropdown (default is false, meaning the filter is disabled and the dropdown will be shown)
$post_type — the current post type

Note that you can also get the current post type using the `get_current_screen()` function and its `post_type` property. That is:

get_current_screen() -> post_type

And I have a feeling that a similar method could be used to disable the category filter as well (needs to be tested).

How to Automate Routine Tasks in WordPress Using the wp-cli Console Utility
How to Automate Routine Tasks in WordPress Using the wp-cli Console Utility

An important “non-news” for WordPress site developers. Have you heard about the command-line utility called “wp-cli”? I’ve been working with WP for a long time, but only found out about it about six months ago.

wp-cli is an amazing tool for developers who manage a bunch of WP sites on their local machine (and not just local ones). Wp-cli allows you to work with WordPress via the command line, minimizing the time you spend on installing the engine, updating it, as well as installing and updating themes and plugins for it.
To be honest, I haven't explored all of its features deeply, but I'll describe the basic functionality and capabilities.

Installation

All instructions are provided for Linux OS.
Download the phar archive with the utility to your machine:

curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

Set execute permissions:

chmod +x wp-cli.phar

Move the file to your system’s program directory (to access it from anywhere):

sudo mv wp-cli.phar /usr/local/bin/wp

read more...

How to Add Custom JavaScript Code to WordPress
How to Add Custom JavaScript Code to WordPress

While working on one of the WP projects, I needed to add some JS code. Normally, for such tasks, I use separate script files. However, in this case, the code was minimal, so I decided to place it directly into the HTML "body" of the document.
To do this, use the following PHP code:

add_action('wp_enqueue_scripts', function(){
	if(!wp_script_is( 'jquery', 'done' ))
	{
		wp_enqueue_script( 'jquery' );
	}
	wp_add_inline_script( 'jquery-migrate', 'alert("Hello")' );
});

Here's what we are doing:

read more...