Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: enqueue script and stylesheet with notice #10

Merged
merged 23 commits into from
Nov 19, 2024

Conversation

JasonTheAdams
Copy link
Contributor

Especially with custom notices, it's very likely that the custom notice will have it's own styles. It's also quite plausible that it will have custom JS as well. It's currently possible, of course, to enqueue these manually in the admin, but it would be a lot of work to get the scripts to only enqueue when the notice is set to display.

This PR introduces AdminNotice::enqueueScript() and AdminNotice::enqueueStylesheet methods. This makes it nice and easy to include a script and/or stylesheet as part of your notice!

AdminNotices::show('custom', function(AdminNotice $notice, NoticeElementProperties $elements) {
    return "<div class='fancy-custom-notice'>
         <marquee>It's <strong>Stellar</strong> at <strong'>StellarWP</strong></marquee>
         <span class='close' {$elements->customCloserAttributes()}>hide</span>
    </div>";
})
    ->custom()
    ->enqueueScript($pluginUrl . 'custom-notice.js')
    ->enqueueStyle($pluginUrl . 'custom-styles.css');

And, of course, if the notice is not going to display — either due to conditions or user dismissal — then the files will not be loaded. Finally, the script is deferred by default, to keep things performant.

Base automatically changed from feature/custom-notice-closing to main November 19, 2024 21:33
Copy link
Member

@borkweb borkweb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great to me!

@JasonTheAdams JasonTheAdams merged commit 30fa50a into main Nov 19, 2024
2 checks passed
@JasonTheAdams JasonTheAdams deleted the feature/notice-scripts branch November 19, 2024 23:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants