Work with CRON in CMS WordPress
Work with CRON in CMS WordPress

In this short note, I’ll show a simple example of working with cron in a popular engine like WordPress.

For a regularly recurring task (i.e., code that should run on a regular basis), you can use the following code snippet:

add_filter('cron_schedules', function ( $schedules ) {
	$schedules['10sec'] = array(
		'interval' => 10,
		'display'  => __('Every 10 sec'),
	);
	return $schedules;
});

add_action('init', function(){
	if(!wp_next_scheduled('post_event_cron_action'))
	{
		wp_schedule_event( time(), '10sec', 'post_event_cron_action');
	}
});

add_action('post_event_cron_action', function () {
	echo 'Mail sent';
	mail('test@example.com', 'Test subject', 'Test body');
});

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...

How to write your own action in Yii2 and reuse it in different controllers
How to write your own action in Yii2 and reuse it in different controllers

Before developing any relatively large product using a framework, I would recommend deeply exploring its capabilities.

Today, we’ll talk about actions — but not the ones that are “hardcoded” into our controllers. Instead, we’ll look at actions that can be reused across different controllers by registering them through the controller’s actions() method:

public function actions()
{
	$actions = parent::actions();
	
	$actions['aj-avatar-upload'] = [
		'class' => 'commonactionuserAjaxAvatarUploadAction',
	];
	$actions['aj-avatar-delete'] = [
		'class' => 'commonactionuserAjaxAvatarDeleteAction',
	];
	
	return $actions;
}

You’ll encounter such "actions" early on, when looking at the configuration file where the error action is defined:

'errorHandler' => [
	'errorAction' => 'site/error',
],

read more...

How to Create a Child Theme in WordPress
How to Create a Child Theme in WordPress

Hello, dear blog readers!

Today, we’ll take a look at a standard topic for most WordPress-related blogs — creating a child theme.

Why might we need to create such a theme:

  1. We need to change the design of specific blog pages without affecting the rest of the theme's functionality.
  2. We need to change the functionality of a specific page — for example, the homepage or the comment form.
  3. We need to add new functionality.

Yes, of course, all of these changes could be made in the current theme — if it was developed by you or built on demand. But if you're using a theme from the WordPress repository, it's better to make changes using a child theme. Because when you modify the parent theme, and then update it, all your changes will be lost.

read more...

Programmatically creating pages in WordPress and displaying them
Programmatically creating pages in WordPress and displaying them

Hello, dear blog readers!

Today we’ll cover another important topic — programmatically creating pages in the WordPress engine, and displaying them at a specified URL.

At first glance, this functionality may seem completely unnecessary. But! What if you need to add some text content on a plugin view page? Sure, we can use our plugin (for example, a contact form) in the form of a shortcode and insert it into the page content. But there are situations when a single shortcode isn’t enough. You may need to create a separate page displaying both a form (e.g., for registration or ordering) and textual instructions. And it’s very important that this text be editable through the admin panel. Why? For example, if it's an order form page, the explanation text might contain current discount information, which can change seasonally or according to the client’s preferences. I believe the idea is clear and the task is understandable (or feel free to comment).
read more...