I code custom plugins for Wordpress. I use Microsoft Visual Studio Code to do the coding and FileZilla to transfer the files.
With almost every php file that I code, I'm not sure if it's something that's happening when I download my plugin and then upload it to a new site, or if it's when I install it on a new site from a zip file, but when I check the code I find that the entire code has ended up with double line breaks!
i.e.,
function gdvcaddons_admin() {
add_menu_page("GD Addons Settings", "GD Addons Settings", "administrator", "gdvcaddons-settings", "gdvcaddons_settings_page");
}
add_action('admin_menu', 'gdvcaddons_admin');
// DEFINE OPTIONS TO BE SAVED //
function gdvcaddons_settings() {
register_setting( 'gdvcaddons-settings-group', 'recaptcha_site_key' );
register_setting( 'gdvcaddons-settings-group', 'recaptcha_private_key' );
}
add_action( 'admin_init', 'gdvcaddons_settings' );
// DECLARE FILE CONTAINING OPTIONS FORM //
function gdvcaddons_settings_page(){
include('inc/gdvcaddons-settings-form.php');
}
Becomes:
function gdvcaddons_admin() {
add_menu_page("GD Addons Settings", "GD Addons Settings", "administrator", "gdvcaddons-settings", "gdvcaddons_settings_page");
}
add_action('admin_menu', 'gdvcaddons_admin');
// DEFINE OPTIONS TO BE SAVED //
function gdvcaddons_settings() {
register_setting( 'gdvcaddons-settings-group', 'recaptcha_site_key' );
register_setting( 'gdvcaddons-settings-group', 'recaptcha_private_key' );
}
add_action( 'admin_init', 'gdvcaddons_settings' );
// DECLARE FILE CONTAINING OPTIONS FORM //
function gdvcaddons_settings_page(){
include('inc/gdvcaddons-settings-form.php');
}
It is quite a problem because it keeps causing Yoast SEO's sitemaps to break and I keep having to open all my php files via FTP and do regex search/replace "\s\n" with "\n".
Does anyone have any idea what might be causing this issue and how I can prevent it from reoccurring?
Try to change transfer type
For single FTPs
Global Settings.
Related
I'm trying to change the flag to a custom flag in svg, and I found this "pll_custom_flag" function in their document(https://polylang.pro/doc/filter-reference/)
There, it says "The filter needs to be added in a plugin or mu-plugin before the action ‘plugins_loaded’ has been fired. It can’t work in the functions.php of a theme."
However, I don't know where and how to add this action specifically.
Here is the action I I customized for my website.
add_filter( ‘pll_custom_flag’, ‘pll_custom_flag’, 10, 2 );
function pll_custom_flag( $flag, $code ) {
$flag[‘url’] = "http://example.com/wp-content/polylang/flags-custom/{$code}.svg";
$flag[‘height’] = 24;
return $flag;
}
I also added two svg images to a new directory(flags-custom) I created under the polylang directory.
I have almost zero knowledge in PHP, and I would greatly appreciate it if anyone could help me on this.
Thank you.
Solution 1:
You need to create plugin for this filter. Create one and put this filter into it.
Solution 2 (simpler):
Put file (example: polylang_filters.php) into /wp-content/mu-plugins with this filter.
After 1 or 2 go to Polylang Settings -> Url modification and click save. No changes required just click save to update urls.
If you have your custom flags in folder they will show up.
PS.
I would prefer to keep my flags in theme folder:
add_filter( 'pll_custom_flag', 'pll_custom_flag', 10, 2 );
function pll_custom_flag( $flag, $code ) {
$flag['url'] = get_template_directory_uri()."/img/lang_flags/{$code}.svg";
$flag['width'] = 20;
$flag['height'] = 20;
return $flag;
}
In my flag folder I have flags:
gb.svg
de.svg
etc...
Good day, I need a plugin to be self-deactivated once the mail is sent to the site owner. However, when I run on local machine the plugins still active in my admin panel.
My code :
if(count($result) == 0){
// Send the mail
send_to_mail();
// self deactivation of this plugin
add_action( 'init', 'deactivate_cronjob_plugin' );
}
// deactivate the plugin
function deactivate_cronjob_plugin(){
if ( is_plugin_active('myPlugin/cron_job.php') ) {
deactivate_plugins('myPlugin/cron_job.php', true);
}
}
I'm using Wordpress 4.9.6, I'm glad if there's any help. Thank you and have a good day.
You need the hole path to the plugin file, like
deactivate_plugins( plugin_basename( __FILE__ ) );
Also the small note that the function is_plugin_active is not necessary. The deactivation works only, if the plugin is active.
I load wordpress with my php code like this to do actions:
define( 'WP_USE_THEMES', false ); // Don't load theme support functionality
require( './wp-load.php' );
How could I also disable plugins temporarily when I want to add posts, etc? This would be to use less server resources...
I don't believe there's actually a good way to do this. The best way is probably to run your function before plugins are loaded. Otherwise you're looking at programatically renaming the /plugins folder or WP_PLUGIN_[DIR/URL] constants which just screams "error prone"
Effectively you can hook into the first action hook available, which is muplugins_loaded: Source. Especially if you have no mu-plugins to be run, it should be run almost instantaneously:
add_action( 'muplugins_loaded', 'run_before_plugins_load' );
function run_before_plugins_load(){
if( condition == met ){
// Insert your post here
if( $post_id = wp_insert_post( $my_post ) ){
exit(); // Post inserted, stop processing anything.
} else {
wp_die( 'Post not inserted' );
}
}
}
From there you'll have inserted a post and stopped any further propagation. Of course, you can replace exit and wp_die with whatever you want - it's just the fastest way I can see to run a WP function without actually loading plugins.
I can use bp_get_activity_content_body() on entry.php
but how can i use it on bp-core-template.php
Sorry for my poor question, i’m very new for php editor.
bp-core-template.php is one of the core files of BuddyPress and that makes it a bad idea to edit the file. Whenever you update Buddypress there is a good chance your changes will be overwritten.
If you are trying to alter the result of a function found in bp-core-template.php then you will want to use one of the filters in that function to safely alter the content "the wordpress way".
So if you want to change the output of the function bp_create_excerpt found on line 877 of bp-core-template.php you could use the filter bp_create_excerpt in the functions.php file of your theme.
In functions.php (example modified from hookr.io):
// define the bp_create_excerpt callback
function filter_bp_create_excerpt( $text, $original_text, $length, $options ) {
if ( function_exists( bp_get_activity_content_body )
$text = bp_get_activity_content_body();
return $text;
};
// add the filter
add_filter( 'bp_create_excerpt', 'filter_bp_create_excerpt', 10, 4 );
EDIT: It should be noted that I do not know what bp_get_activity_content_body() will return in this context and I would probably just return $original_text if I didn't want a truncated excerpt. My intent is to highlight how you should be changing output of things from a plugin's core files.
So here i find myself, again, struggling with wordpress wysiwyg editor.
a client of mine requested to migrate his website to WP. No probs, a breeze :).
Really was easy, migrated from one DB structure to the other, and everything went OK.
Now I have a problem. The old site, used an editor that added <br> and <p> tags to the content in order to format it (sounds legit to me). But wordpress will not allow these tags. whenever the client tries to edit a post, WP removes all the HTML tags it considers "illegal".
So I went on the search. First I tried to install some recommended plugins I found for this problem (such as this one). Didn't work at all for me (for some others it did i believe)...
Then I found a post that told me to add a function to the function.php file which will remove the filters :
function mod_mce($initArray) {
$initArray['verify_html'] = false;
return $initArray;
}
add_filter('tiny_mce_before_init', 'mod_mce');
and also this:
function my_tinymce( $init ) {
$ext = 'div[id|name|class|style]';
if ( isset( $init['extended_valid_elements'] ) ) {
$init['extended_valid_elements'] .= ',' . $ext;
} else {
$init['extended_valid_elements'] = $ext;
}
return $init;
}
add_filter( 'tiny_mce_before_init', 'my_tinymce' );
functions from this thread.
Nope, didn't work also...
Someone - any idea? It seems so silly, but there is so much debate around this subject...
Thanks
You may try this to remove filters that wpautop uses to filter content and excerpt, just put these in your funcions.php file
remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_excerpt', 'wpautop' );
Reference: WordPess wpautop
To allow older content with p and br to load in tinyMCE
function my_tinymce_config( $init ) {
$init['remove_linebreaks'] = false;
$init['convert_newlines_to_brs'] = true;
$init['remove_redundant_brs'] = false;
return $init;
}
add_filter('tiny_mce_before_init', 'my_tinymce_config');
Reference: tinyMCE Configuration look at Cleanup/Output and try playing with these.
Another way could be helpful Reference
tinyMCE.init({
...
verify_html : false
});