Filter uncanny-automator

automator_on_activate_redirect_to_dashboard

Filters whether to redirect the user to the dashboard after activation.

add_filter( 'automator_on_activate_redirect_to_dashboard', $callback, 10, 1 );

Description

Filters whether to redirect to the dashboard after activating the Automator plugin. Developers can return `false` to prevent the redirect. This hook fires after the plugin is successfully activated and before the redirect logic executes.


Usage

add_filter( 'automator_on_activate_redirect_to_dashboard', 'your_function_name', 10, 1 );

Return Value

The filtered value.


Examples

<?php
/**
 * Prevent Automator from automatically redirecting to the dashboard upon activation.
 *
 * This example demonstrates how to use the 'automator_on_activate_redirect_to_dashboard'
 * filter to disable the default redirect behavior. This might be useful in scenarios
 * where you want to perform custom actions immediately after plugin activation without
 * immediately taking the user to the dashboard, or if you have a custom activation
 * process that handles redirects.
 */
add_filter( 'automator_on_activate_redirect_to_dashboard', function( $should_redirect ) {
	// In this specific example, we're always returning false to disable the redirect.
	// In a real-world scenario, you might check certain conditions here, such as
	// a specific user role, a custom setting in the database, or a flag set elsewhere
	// in your plugin or theme.
	// For instance:
	// if ( current_user_can( 'manage_options' ) && get_option( 'automator_disable_activation_redirect', false ) ) {
	//     return false;
	// }

	// For this example, we'll simply disable the redirect unconditionally.
	return false;
}, 10, 1 );
?>

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/class-automator-load.php:293

public function automator_activated( $plugin ) {

		// If it's not Automator, bail
		if ( plugin_basename( AUTOMATOR_BASE_FILE ) !== $plugin ) {
			return;
		}

		// If disbaled by filter
		if ( false === apply_filters( 'automator_on_activate_redirect_to_dashboard', true ) ) {
			return;
		}

		// Check if the current user can activate plugin
		if ( ! current_user_can( 'activate_plugins' ) ) {
			return;
		}

		// If activated via AJAX or REST
		if ( ( defined( 'DOING_AJAX' ) && DOING_AJAX ) || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
			return;
		}

		// If activated via CRON
		if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
			return;
		}

		// If not defined cli
		if ( function_exists( 'php_sapi_name' ) && php_sapi_name() === 'cli' ) {
			return;
		}

		// Bail if from Codeception WPTestCase.
		if ( class_exists( 'CodeceptionTestCaseWPTestCase' ) ) {
			return;
		}

		// Bail if in WP CLI mode.
		if ( defined( 'WP_CLI' ) && WP_CLI ) {
			return;
		}

		// If HTTP_USER_AGENT is missing for an automated script
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
			return;
		}

		$checked = filter_input( INPUT_POST, 'checked', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY );

		// Bail if bulked activated and there are more than 1 plugin.
		if ( is_array( $checked ) && count( $checked ) >= 2 ) {
			return;
		}

		// Bail if not from `wp-admin/plugins.php` (e.g coming from an ajax, or unit test)
		if ( ! check_admin_referer( 'activate-plugin_' . $plugin ) ) {
			return;
		}

		// If the site is not previously connected, let's redirect to Setup Wizard
		if ( class_exists( 'Uncanny_AutomatorApi_Server' ) && empty( Api_Server::get_license_key() ) ) {
			wp_redirect( esc_url_raw( admin_url( 'admin.php?page=uncanny-automator-setup-wizard' ) ) ); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect
			exit();
		}

		// Else, redirect back to Dashboard
		wp_redirect( esc_url_raw( admin_url( 'admin.php?page=uncanny-automator-dashboard' ) ) ); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect
		exit();
	}

Scroll to Top