DB

How to connect to multiple databases in Yii2
How to connect to multiple databases in Yii2

To connect to multiple databases in the Yii2 framework, you need to do the following:

1. Create two database configuration files in your site’s config. I keep the connections in separate files, and they look like this (example).

Connection to the first database:

return  [
	'class' => 'yiidbConnection',
	'dsn' => 'mysql:host=127.0.0.1;dbname=work_db1',
	'username' => 'root',
	'password' => '',
	'charset' => 'utf8',
	'tablePrefix' => 'tbl_',
];

Connection to the second database:

return  [
	'class' => 'yiidbConnection',
	'dsn' => 'mysql:host=127.0.0.1;dbname=work_db2',
	'username' => 'root',
	'password' => '',
	'charset' => 'utf8',
	'tablePrefix' => 'tbl_',
];

2. In the main config file, register both connections:

'db' => require(__DIR__ . '/db.php'),
'db2' => require(__DIR__ . '/db2.php'),

read more...

How to Get Your Own Post Type in WordPress by Filtering by the Right Taxonomy
How to Get Your Own Post Type in WordPress by Filtering by the Right Taxonomy

To retrieve a specific post type in WordPress filtered by a custom (or built-in) taxonomy, use the following snippet:

$Posts = get_posts(array(
	'post_type' => 'my-post-type',
	'order' => 'ASC',
	'tax_query' => array(
	array(
			'taxonomy' => 'my-taxonomy',
			'field' => 'slug',
			'terms' => 'event'
		)
	),
	'meta_query' => array(
		'AND',
		array(
			'type' => 'NUMERIC',
			'key' => 'event_date',
			'compare' => '<', 'value' => time()
		),
		array(
			'type' => 'NUMERIC',
			'key' => 'is_archive',
			'compare' => '==',
			'value' => 0
		)
	)
));

read more...

Plugin Interaction with WordPress Database. Part 2
Plugin Interaction with WordPress Database. Part 2

Continuing our series of articles on working with the database in WordPress. Today, we’ll talk about creating, updating, deleting, and retrieving data from the database.
Please note that we are not working with WordPress system tables, but with a custom one.
The table dump is shown below:

CREATE TABLE IF NOT EXISTS `wp_plance_text_shortcodes` (
  `sh_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `sh_title` varchar(150) COLLATE utf8mb4_unicode_ci NOT NULL,
  `sh_code` varchar(25) COLLATE utf8mb4_unicode_ci NOT NULL,
  `sh_description` text COLLATE utf8mb4_unicode_ci NOT NULL,
  `sh_is_lock` tinyint(1) unsigned NOT NULL,
  `sh_date_create` int(10) unsigned NOT NULL,
  PRIMARY KEY (`sh_id`)
) ENGINE=InnoDB;

This is the current structure of the table used in my plugin “My Text Shortcodes”.

read more...

Plugin interaction with WordPress database. Part 1.
Plugin interaction with WordPress database. Part 1.

Hello, dear blog readers!
Let’s expand our WordPress programming knowledge a bit and look into how plugins interact with the database.

Out of the box, WP has pretty decent capabilities. I’m referring to things like creating custom taxonomies, terms, and linking them with additional data using metadata. It’s very convenient — once you write a dozen functions, you no longer need to worry about creating interfaces or even some of the internal app logic.

But for me, as someone still "new" to WordPress, it feels more natural to work with clearly structured database tables.
So in this article, I propose to explore that approach — based on one of my plugins called “my-simple-form,” slightly simplified for the purpose of the example.

read more...