$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>css - Completely isolate a plugin view so it doesn't load the theme|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

css - Completely isolate a plugin view so it doesn't load the theme

matteradmin8PV0评论

We're making a WordPress plugin that generates an iframe with a shortcode, and that iframe that should work and display the same no matter what site, page or theme it's used on. The view is located at an URL inside the plugin folder.

Similarly, the plugin's styles and scripts shouldn't affect the site as a whole.

I'm not sure what the right way to do that isolation is. One way would be not to use wp_head() or wp_footer() and put my styles and scripts directly in the HTML instead of enqueuing them.

That works, but I can't help but wonder if there could be a better "WordPress Way" to do this.

I also thought of just mass-dequeuing every script and stylesheet only at this URL, but the theme would still be free to inject arbitrary HTML using hooks.

What's the right way to get a "clean slate" to work on for my plugin page?

We're making a WordPress plugin that generates an iframe with a shortcode, and that iframe that should work and display the same no matter what site, page or theme it's used on. The view is located at an URL inside the plugin folder.

Similarly, the plugin's styles and scripts shouldn't affect the site as a whole.

I'm not sure what the right way to do that isolation is. One way would be not to use wp_head() or wp_footer() and put my styles and scripts directly in the HTML instead of enqueuing them.

That works, but I can't help but wonder if there could be a better "WordPress Way" to do this.

I also thought of just mass-dequeuing every script and stylesheet only at this URL, but the theme would still be free to inject arbitrary HTML using hooks.

What's the right way to get a "clean slate" to work on for my plugin page?

Share Improve this question asked Mar 15, 2019 at 19:24 ArianeAriane 1012 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Preferably don't load the plugin URL directly (well, unless it is a public URL and you can pass whatever data you need as querystrings) as this makes it difficult to load WordPress and access any stored data (as the path is not always determinable), but instead either:

  1. load the view via a querystring and check for it in WordPress load OR
  2. similar, but add it to an AJAX action querystring function.

Option 1. via /any-page/?my-action=trigger

add_action('init', 'my_plugin_view');
function my_plugin_view() {
    // check for querystring trigger
    if (!isset($_REQUEST['my-action']) || ($_REQUEST['my-action'] != 'trigger-value')) {return;}

    if (!is_user_logged_in()) {
       wp_die('You need to be logged in to do this.');
    } else {
        // ... plugin view output code ...
    }
    exit;
}

Option 2. via /wp-admin/admin-ajax.php?action=my_plugin_view

// logged in users
add_action('wp_ajax_my_plugin_view', 'my_plugin_view');

function my_plugin_view() {
    // ... plugin view output code ...
    exit;
}

// logged out users (optional) 
add_action('wp_ajax_nopriv_my_plugin_view', 'my_plugin_view');
function my_plugin_logged_out_message() {
    wp_die('You need to be logged in to do this.');
}

As you can see there is a difference in the target URL that is used as well as how logged out users are handled (if needed, in this example it is assumed the user must be logged in, but this distinction is not necessary for compltely public view.)

Either are viable options, but probably the AJAX method is the more common "WordPress way" of doing this. Of course any plugin scripts and styles would need to be added directly to the output instead of enqueueing them as normal.

Codex Reference: AJAX in Plugins

Post a comment

comment list (0)

  1. No comments so far