I created a WP plugin, with minimal PHP code - it's almost JS. When I upload files manually to the WP plugins directory, it works.
But when I'm trying to zip the plugin and install it from a zip file, it doesn't work.
The PHP code itself is minimal.
I created a header with minimal requirements and some code to return html and to registre JS and CSS files.
There are other files too: image files, JS files (2 of them)
When I'm trying to install them, I get a message: The link you followed has expired.
Please try again.
I did everything according to tutorials, checked with several of them, and I have no Idea what I'm doing wrong.
Here's my code:
<?php
/**
* Plugin Name: Plugin name
* Description: Plugin description
* Version: 1.0.1
* Author: Author
*/
function dare2care( $atts, $content, $tag ){
wp_register_style( 'dare2care', 'https://cdn.jsdelivr.net/npm/bulma#0.9.4/css/bulma.min.css' );
wp_enqueue_style('dare2care');
wp_enqueue_script('dare2care-quiz', plugins_url( 'dare2care-quiz.js', __FILE__ ), in_footer:true);
$image_base_url = plugins_url( 'images/', __FILE__ );
$content = '
<div id="quiz" class="container">
... truncated...
</div>
';
return $content;
};
add_shortcode('d2c', 'dare2care');
function register_shortcodes(){
add_shortcode('d2c', 'dare2care');
};
add_action( 'init', 'register_shortcodes');
?>
You have to add this to your theme functions.php
#ini_set( 'upload_max_size' , '120M' );
#ini_set( 'post_max_size', '120M');
#ini_set( 'max_execution_time', '300' );
It's a known issue that needs a deeper approach to avoid. I don't know if I can analyze the whole situation assuming to stackoverflow rules.
Related
I am using WordPress and I installed the Woocommerce plugin.
I added the template folder in my theme and started to customize my theme.
Now, I have to remove the Woocommerce.css from my theme and I found code on the official website here
I added the same code in the function.php
add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );
or
add_filter( 'woocommerce_enqueue_styles', '__return_false' );
but both answers are not working. I have installed the 5.7.1 Woocommerce.
How can I solve this issue?
function.php
function customtheme_scripts() {
wp_enqueue_style( 'customtheme-style', get_stylesheet_uri(), array(), time() );
wp_style_add_data( 'customtheme-style', 'rtl', 'replace' );
wp_enqueue_script( 'customtheme-navigation', get_template_directory_uri() . '/js/navigation.js', array(), _S_VERSION, true );
if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
wp_enqueue_script( 'comment-reply' );
}
wp_dequeue_style( 'customtheme-woocommerce-style' );
}
add_action( 'wp_enqueue_scripts', 'customtheme_scripts' );
view source
The domain name is just an example.
This answer has been fully tested on woocommerce 5.x+ and works fine on the default woo style sheets! If you're using a custom theme and/or custom style sheet then you may encounter some discrepancies.
What you see on the documentation page, no longer works on woo 4+, according to their github page.
So you need to dequeue its styles!
wp_dequeue_styleDocs
So if you only want to remove woocommerce.css file, then you could do this:
add_action('wp_enqueue_scripts', 'removing_woo_styles');
function removing_woo_styles()
{
wp_dequeue_style('woocommerce-general'); // This is "woocommerce.css" file
}
However, if you want to remove all of the style sheets loaded by woo, then you could use this:
add_action('wp_enqueue_scripts', 'removing_woo_styles');
function removing_woo_styles()
{
wp_dequeue_style('wc-block-vendors-style');
wp_dequeue_style('wc-block-style');
wp_dequeue_style('woocommerce-general');
wp_dequeue_style('woocommerce-layout');
wp_dequeue_style('woocommerce-smallscreen');
}
If you still can see the styles, then try to clean your cache.
UPDATE related to the question "custom style sheet"
At the time that I was writing the answer you had not provided any screenshot of your style sheet, nor did you say anything about using a custom style sheet. That's why you were not able to get it to work.
Please do NOT copy/paste if you're using a custom style sheet, like the custom css file used in the question. wp_dequeue_style function takes your style sheet handle as the argument. So please read the documentation first. You're using a custom handle name (i.e "customtheme-woocommerce-style"), therefore, you need to use that handle name.
add_action('wp_enqueue_scripts', 'removing_woo_styles');
function removing_woo_styles()
{
wp_dequeue_style('customtheme-woocommerce-style'); // This is your "custom style sheet" file.
}
Also note that commenting out the enqueue section in the main file (i.e inc/woocommerce.php) may work temporarily but on the next woo update, it'll come back again. So, it's recommended to avoid updating your template files as much as possible unless you really have to which is not the case here!
I am trying to enqueue an external plain JavaScript file from within my plugin php, to add within my footer of the WP theme. Is this possible?
I've tried the below, but test is failing as no alert is firing...
<?php
/**
Plugin Name: Add Footer JS
*/
add_action( 'wp_footer', 'my_footer_scripts' );
function my_footer_scripts(){
?>
<script>alert( 'Hi Roy' ); </script> // just a test
<?php
}
So basically; the JS would be at:
http://dev.website.com/wp-content/plugins/mykewlplugin/js/script-support.js
And this PHP, at:
http://dev.website.com/wp-content/plugins/mykewlplugin/support.php
And core plugin PHP/functionality at:
http://dev.website.com/wp-content/plugins/mykewlplugin/mykewlplugin.php
Make sure that your currently active theme is making use of the wp_footer() and wp_head() functions, as those are required to utilize the appropriate action hooks. With that said, you should really properly enqueue your assets. Anybody else working on the project (including your future self) will be appreciative of that!
This involves utilizing the wp_enqueue_scripts hook and the wp_enqueue_script() function (which relies on both the wp_head() and wp_footer functions).
The proper way to do this would be like the following:
add_action( 'wp_enqueue_scripts', 'no_spex_footer_js' );
function no_spex_footer_js(){
wp_enqueue_script( 'no-spex-footer-js', '/path/to/your/file.js', array(), null, true );
}
Note all the passed arguments, in order:
a unique handle
the path to your actual file
an array of dependencies by handle (if you rely on jQuery, change this to array('jquery'), etc.
version of the file. I use filemtime( plugin_dir_path( __FILE__ ) . 'assets/file-name.js' typically.
in_footer - This is the important one, and determines if the file should be loaded into the header or footer.
If you're sure your theme is making use of wp_head() and wp_footer(), and the file isn't working - it means your plugin isn't configured/activated properly, at which point I'd recommend another cursory glance at the Writing A Plugin documentation and the File Header Requirements.
For your specific case, something like this should get you started:
mykewlplugin.php:
<?php
/**
* Plugin Name: My Kewl Plugin
* Description: Does Kewl Things
* Version: 0.1
*/
add_action( 'wp_enqueue_scripts', 'my_kewl_plugin_footer_js' );
function my_kewl_plugin_footer_js(){
wp_enqueue_script( 'my-kewl-plugin-footer', plugins_url( '/js/script-support.js', __FILE__ ), array(), filemtime( plugin_dir_path( __FILE__ ) . 'js/script-support.js', true );
}
?>
<?php
/*
* Plugin Name: Add Fontawesome Animation
* Plugin URI: ...
* Description: This plugin is used to animate Fontawesome icons
* Version: 1.0.0
* Author: ...
* Author URI: ...
* License: GPLv2
* * */
// Enqueue font-awesome-animation.css
add_action( 'wp_enqueue_style', 'add_fontawesome_animation' );
function add_fontawesome_animation() {
wp_enqueue_style('font_awesome_animation', get_stylesheet_directory_uri() . '/css/font-awesome-animation.css' );
}
Objective: load font-awesome-animation.css so FontAwesome icons can animate.
Using: PhpStorm and just getting started with Genesis. I am using the genesis-sample child theme and for the most part all is going reasonably well publishing posts and learning to use code to mod the templates.
The FontAwesome icons will animate when I #import url("css/font-awesome-animation.css") into the style.css file else I have not been able to enqueue font-awesome-animation.css from functions.php or the plugin as shown above. I get no observable errors in the editor or in the page nor do I get the typical all white page when there are errors in the PHP. Something is FUBAR and I don't know how to do this type of debugging in WordPress yet so I need to know what may be wrong with the code and suggestions about how to debug this type of SNAFU. Is there some type of Response.Write I can use to show me pathing and such?
Try changing the hook you are using to wp_enqueue_scripts. From the codex:
wp_enqueue_scripts is the proper hook to use when enqueuing items that
are meant to appear on the front end. Despite the name, it is used for
enqueuing both scripts and styles.
Also get_stylesheet_directory_uri() will not work for you if you are making a plugin. That is only for use in themes/child themes. If you are calling this from your main plugin file you can use plugin_dir_url( __FILE__ ) instead. Again, from the codex:
Gets the URL (with trailing slash) for the plugin FILE passed in
In this case we are passing the PHP magic constant __FILE__ to get the uri for the main plugin file with a trailing slash.
So your code would look like the following:
<?php
// Enqueue font-awesome-animation.css
add_action( 'wp_enqueue_scripts', 'add_fontawesome_animation' );
function add_fontawesome_animation() {
wp_enqueue_style('font_awesome_animation', plugin_dir_url( __FILE__ ) . 'css/font-awesome-animation.css' );
}
In my website, www.piorganic.in, the stylesheet and scripts are not loaded properly. Including images too.
Upon inspection with network tools, the path to the files were coming like
http://www.piorganic.in/wp-admin/wp-content/plugins/flickr-badges-widget/css/widget.css?ver=0.7
instead of
http://www.piorganic.in/wp-content/plugins/flickr-badges-widget/css/widget.css?ver=0.7
Someone has worked on this somefile and now i need guidance which file adds this name wp-admin to the url.
Hope i was clear.
Inshort, where are the urls setup with wordpress for stylesheet and scripts
Here is snapshot on icognito mode
How Include a css and js in theme
Step 1: goto header.php file and copy all path
step 2 : open functions.php file and enqueue all file here
/**
* Include CSS file for theme.
*/
function mytheme_scripts() {
wp_register_style( 'my-styles', get_template_directory_uri().'css/mycss.css' );
wp_register_script( 'my-Jscript', get_template_directory_uri().'js/myjs.js' );
wp_enqueue_style( 'my-styles' );
wp_enqueue_script( 'my-Jscript' );
}
add_action( 'wp_enqueue_scripts', 'mytheme_scripts' );
How Include a css and js in plugin
Step 1 : goto wp-content/plugins/yourplugin/mainfile.php.
/**
* Include CSS file for MyPlugin.
*/
function myplugin_scripts() {
wp_register_style( 'flicker-widget-styles', plugin_dir_url( __FILE__ ) . 'css/widget.css' );
wp_enqueue_style( 'flicker-widget-styles' );
}
add_action( 'wp_enqueue_scripts', 'myplugin_scripts' );
Step 2: Clear cache and refresh your tab using ctrl+f5.
Step 3: check console of browser check any error there.
Solution for above screenshot
Problem : Resource load from path contains wp_admin
Step 1 : open you wp_config and check both url or base url in wp_option in db
define('WP_HOME','http://example.com');
define('WP_SITEURL','http://example.com');
step 2 : Login to admin dashboard and go to general -> setting check both url.
step 3 : open functions.php and check
define('WP_HOME','http://example.com');
define('WP_SITEURL','http://example.com');
If any include wp_admin then remove it.
I have a project to complete where sites within a WordPress Multisite blog will automatically add users to the corresponding sites (where the plugin is enabled)
I hope I worded that correctly.
The problem: The wpmu_new_user hook does not fire inside of a plugin but WILL inside of functions.php
This is my code:
add_action( 'wpmu_new_user', 'register_hack_action', 10, 1 );
add_action( 'wpmu_activate_user', 'register_hack_action', 10, 1);
function register_hack_action( $user_id ) {
$this_id = get_current_blog_id();
if ( !defined('ABSPATH') ) {
// do nothing
} else {
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
}
$blog_list = get_blog_list( 0, 'all' );
foreach ($blog_list AS $blog) {
switch_to_blog($blog['blog_id']);
if ( is_plugin_active( 'register-hack/register-hack.php' ) ) {
// add user to blog
add_user_to_blog($blog['blog_id'], $user_id, 'subscriber');
}
}
}
This works perfectly fine when you add the snippet to functions.php. But when you add it to a plugin (which goes into wp-content/plugins) and then is activated on certain sites, just will not work. if you can try it for yourself you'll see what i mean.. I don't understand why it will not work. I need it to be inside of the plugin and work.
Only some extensive debugging could answer this... But this kind of stuff is better placed inside a Must Use plugin.
Must-use plugins (a.k.a. mu-plugins) are plugins installed in a special directory inside the content folder and which are automatically enabled on all sites in the installation. Must-use plugins do not show in the default list of plugins on the Plugins page of wp-admin – although they do appear in a special Must-Use section – and cannot be disabled except by removing the plugin file from the must-use directory, which is found in wp-content/mu-plugins by default.
I don't understand why you're using that include_once, please, test it removing that.
Especulations:
as a normal plugin, it should be Network Activated and try to encapsulate the actions with:
add_action( 'plugins_loaded', function() {
add_action( 'wpmu_new_user', 'register_hack_action', 10, 1 );
add_action( 'wpmu_activate_user', 'register_hack_action', 10, 1);
});
maybe you're putting inside the main site theme?