Using dependencies in wp_register_style() for wordpress - function

I am trying to get a custom css file to load after the plugin stylesheets. I was thinking I could use the $deps parameter of wp_register_style(), but the css file does not load at all when I add the array(). This happens no matter what is included in the $deps array(), ie (array('style')
, array('style.css')).
Is there an issue with the call, or a better way of doing this?
In my functions.php
// Load custom css
add_action('wp_enqueue_scripts', 'prefix_add_my_stylesheet');
function prefix_add_my_stylesheet() {
wp_register_style( 'custom-supersized-styles', get_template_directory_uri(). '/css/custom-supersized-styles.css', array('style','supersized');
wp_enqueue_style( 'custom-supersized-styles' );
}

If you're using the WP Supersized plugin try registering your function like so:
// Load custom css
add_action('wp_enqueue_scripts', 'prefix_add_my_stylesheet', 999);
The dependencies array (the parameter you're attempting to use to add supersized with) depends on supersized having already been registered by WordPress. If you set the priority of your own prefix_add_my_stylesheet to a higher number, it should then load after the plugin has registered and loaded its CSS (and thus will be available).
In addition, you can remove the style as a dependency. (style is never registered by WordPress as a dependency handler, and your enqueued scripts / css should be loading after style.css loads anyway).
Hope that helps!

Related

Enqueue styles and scripts for WordPress Gutenberg block

what are the best or the recommended way to Enqueue styles and scripts for WordPress Gutenberg block ?
i see some developers recommend enqueue_block_assets hook and others recommend init hook with refering to styles and scripts handles in register_block_type php function
What should i use ?
It does depends on the use case as not all cases are the same but I believe and stand to be corrected, Wordpress now recommends using register_block_type to add scripts and css instead.
This is because adding it via block registration allows Wordpress to add the css only when a block is used.
Test it and see. Add block assets using enqueue_block_assets and it will load on every page even if the block is not loaded on the page.
The styles and scripts added below will only be added when the block is added to a page.
My answer is based on some of my own testing recently. CSS Added via blocks using the register block are injected when a block is added not at load time like with enqueue_block_assets
Also see this article as a reference in terms of it now being recommended by Wordpress
register_block_type('namespace/your-block', array(
'style' => 'view-style-handle',
'editor_style' => 'editor-style-handle',
'editor_script' => 'block-script-handle'
));

Is it possible to create a plugin inside a plugin with CakePHP3?

I would like to create a plugin inside a plugin with Cakephp3. I found a solution for Cakephp2 but it doesn't seem to work in Cakephp3:
Is it possible to create a plugin inside a plugin with CakePHP?
How can I do this in Cakephp3?
I'm going to assume your question regards having the plugin loaded in your CakePHP 3.x application, not creating the plugin :-)
Note: this answer assumes you installed Cake using composer.
As an example, let's say we wanted to create a plugin, Themes, and this plugin was going to contain other plugins, Blue and Red.
By convention, the Themes plugin should be contained in your_app/plugins/Themes and the Blue and Red plugins could be contained in your_app/plugins/Themes/plugins/Blue and your_app/plugins/Themes/plugins/Red respectively.
In your_app/config/bootstrap.php , add the following:
Plugin::load('Themes', ['bootstrap' => true]);
(See https://book.cakephp.org/3.0/en/plugins.html#plugin-configuration for info regarding plugin configuration)
The code above tells Cake to load the Themes plugin, and to look for and load the plugin's bootstrap file.
If you haven't already done so, create the Themes plugin's bootstrap file at your_app/plugins/Themes/config/bootstrap.php and have it looking similar to:
<?php
use Cake\Core\Plugin;
// load the red and blue child plugins
Plugin::load('Themes/plugins/Red');
Plugin::load('Themes/plugins/Blue');
Important: Since you're trying to write the plugin(s) manually and not installing via composer, you need to modify your_app/composer.json to contain something like:
"autoload": {
"psr-4": {
"App\\": "src",
"Red\\": "./plugins/Themes/plugins/Red/src",
"Blue\\": "./plugins/Themes/plugins/Blue/src"
}
}
(See https://book.cakephp.org/3.0/en/plugins.html#autoloading-plugin-classes for more info on autoloading plugin classes).
Now, from within your_app/ run:
php composer.phar dumpautoload
(or the equivalent command, depending on how composer was installed on your machine)
This tells composer to refresh its autoloading cache. If you were to inspect your_app/vendor/cakephp-plugins.php, you should see that a path to the Themes plugin's folder has been added to a pre-existing list of plugin paths.
Now, from within your app's main controllers, you should be able to have stuff like:
public function initialize()
{
// load (supposedly-existing) components from the "Red" or "Blue" themes
// load GradientComponent of the "Red" theme
$this->loadComponent('Red.Gradient');
// load ColorComponent of the "Blue" theme
$this->loadComponent('Blue.Color');
// use what you asked for...
$this->Color->someMethod(['data']);
parent::initialize();
}
Also, to use view files (that you'd expect theme plugins to provide :-) ):
public function beforeRender(Event $event)
{
// use the "home" layout from the Red theme
$this->viewBuilder()->setLayout('Themes/plugins/Red.home');
parent::beforeRender($event);
}

Convert php or phtml file to css

i develop a admin panel for manage colors in frontend. I don't want to add all color variables in theme files ( like header.phtml, footer.phtml, etc ) but i want to generate a file with all css class and color variables.
Ex.
<?php
$color = Mage::getStoreConfig('themeadmin/frontend/general_color');
echo '.top-header-container { background-color : #'.$color.'; } .menu { background-color : #'.$color.'; }';
?>
How i can do this without add this in theme file ? Can i generate a Css file ? what is the best way ?
Thanks
You can very easily simply call a .php file (or a controller action) as the actual stylesheet to be used.
ref: Include: css with php file extension?
So, considering that possibility you can do the following:
Create a controller action in your module.
Code for this action to simply output a valid formatted stylesheet
Add that action to your layout, via xml, or programatically inject the sheet into the layout, potentially via an observer event/model/controller action
example:
Mage::app()->getLayout()->getBlock('head')->addCss(
'dyncatprod/adminhtml.css'
);
Another possibility is to hook into the adminhtml save functionality (event should do) and generate one specific .css file form the admin options selected. The end result is one file, statically named, that you woudl then include normally. Make sure you provide a default base file for pre-saved instances.
This way would always ensure one file with the correct values, as a flat stylesheet.

Register a js file independently in Assets

I'm developing a module where I've some custom js functions to be use in a form inside a Dialog.
I successfully loaded as inline script in the view that launch the dialog that include the form with something like:
$script = <<< JS
var scheda=$("table#schedaTable");
var idtum=scheda.attr('idtum');
scheda.find("i.modifica.concessione").on('click', function() {
$("#concessioneDialog").load("/cimiteriale/concessioni/edit-concessione?idtum="+idtum).dialog("open");
});
JS;
$this->registerJs($script, $this::POS_END);
but for several reasons I prefer to use an external file published in assets so my question is:
Is it possible to publish an external js file with MyModuleAssets.php and register it later in the view independently from the other js assets, where needed?
In fact I need to close and reopen the dialog with other ids and so I need to register the js again several time.
Could someone give me an hint and maybe some pieces of the core code?
Thanks in advance
Luca
Assets are published as a package. So if you want some JS/CSS to be registered somewhere and others in another situations, you should create different packages and register each of them when needed.

wp_enqueue_style Does Not Behave As Documented

I have the following written in a driver:
add_action( 'wp_enqueue_scripts', 'add_css_file' );
function add_css_file()
{
// Tried this prior:
// $path = ABSPATH . "/wp-content/plugins/this_plugin/css/";
// wp_register_style( 'css_file', $path.'css_file.css' );
wp_register_style( 'css_file', 'http://subdomain.mysite.com/wp-content/plugins/this_plugin/css/css_file.css' );
wp_enqueue_style( 'css_file' );
}
The string "css_file" does not appear in the View Source HTML and the CSS rules defined therein do not take effect.
Have you included wp_head() in the <head> of your page? wp_head() will add your queued styles and scripts to the page.
The problem for me lay in the fact that I am modifying the Admin screen rather than the display of a Page or Post to viewers.
Replace:
wp_enqueue_script(...)
With:
admin_enqueue_script(...)
Best of luck.
I have just tried this snippet in my site, and it is working properly. You may get conflict with some other plugin/code.
Also make sure you don't have any Cache enabled, and if so, try to clean up the cache. Also try to clean out your browser cache.
Finally, if non of the above works for you, try to de-activate your plugins, one by one in order to see if some of the plugins conflicts with this function, and in last step try to change theme, also to make sure the current theme doesn't conflict with this function.