Elementor and WordPress - php special characters must be escaped - php

I am trying to insert this code to > Elementor > Custom Code to change the Sale Badge Text in WooCommerce
add_filter(‘woocommerce_sale_flash’, ‘edit_sale_badge’); function edit_sale_badge() { return ‘TEXT-GOES-HERE’; }
From this page: https://pluginsforwp.com/blog/change-sale-badge-woocommerce/ – But I dont know how to make it work, can anyone help me? Can PHP be added to Elementor custom code? Thanks!
wrap the code with th PHP but getting the error: php special characters must be escaped

Did you wrap your code with
<?php and ?>
So that it looks like the following:
<?php
add_filter('woocommerce_sale_flash', 'edit_sale_badge');
function edit_sale_badge() {
return 'TEXT-GOES-HERE';
}
?>
In your Link, they add it to the Themes Functions:
functions.php
Why not do it also there?
Note: there is a difference between the following symbols `, ´ and '. You want to use the last one.

You cant insert php in Elementor Custom Code area.. You must add this via functions.php in your child theme. Or you can add this via other plugins (2).
Look also here: https://elementor.com/help/custom-code-pro/
(2): https://de.wordpress.org/plugins/insert-php-code-snippet/

Related

Allow PHP codes in Wordpress posts & pages (Using functions.php)

I need to write pure PHP codes in WP posts - not using a plugin or snippets etc.
I know this Q has been asked before, but all were answered by using plugins etc.
There were some plugins like PHP-Exec that could do the job, but due to latest WP/PHP updates they no longer work.
Is there any way to write some functions in FUNCTIONS.PHP to allow this?
So a code like this can work as a WP post?
<p>
My text paragraph.
</p>
<?php
// Custom PHP codes - vary in wp posts
echo "Anything ... ";
?>
<p>
My second text paragraphs.
</p>
Thanks a lot guys!
Best approach would be to wrap your PHP code in a shortcode inside functions.php and then call it out of your page.
function anything_function() {
// your actual functionality.
$message = "Anything ... ";
return $message;
}
// register shortcode
add_shortcode('anything', 'anything_function');
And then you can use the [anything] shortcode inside your templates.
Please refer to Shortcode API for more information.

WordPress Divi shortcode adding

WordPress Divi Theme shortcode adding problem
My Divi version is 3.20.1. I am trying to add my own custom shortcode in the website. However, when I add this shortcode, the elements displayed using this shortcode appear both in the "Edit page" area top section besides the main page.
add_shortcode( "Btx_Show_Testimonial_Main_Page", 'lantry_btx_fun_Main_Page_Show_Testimonial');
function lantry_btx_fun_Main_Page_Show_Testimonial(){
include_once LANTRY_BITECHX_SHORTCODE_DIR_PATH."views/Main_Page_testimonial_show.php";
}
My question is, how can I remove this from the "Edit page" section ??
I provided some screenshot.
Divi module Image Option Select
Show top of the post page
When I remove Divi this problem will be solved. But I need to use Divi.
You can check if a adminuser is logged in and if not execute your function hope this helps but now you have to always log out or use incognito mode to see if its there:
add_shortcode( "Btx_Show_Testimonial_Main_Page", 'lantry_btx_fun_Main_Page_Show_Testimonial');
function lantry_btx_fun_Main_Page_Show_Testimonial(){
if (current_user_can( 'update_core' )) {
return;
}
include_once LANTRY_BITECHX_SHORTCODE_DIR_PATH."views/Main_Page_testimonial_show.php";
}
I don't think the problem comes from DIVI but from your shortcode. Please attach the content of your file Main_Page_testimonial_show.php
The shortcode callback shouldn't produce an output but should return the result.
You can find this on documentation website here. Please pay attention to
Note that the function called by the shortcode should never produce an
output of any kind. Shortcode functions should return the text that is
to be used to replace the shortcode. Producing the output directly
will lead to unexpected results. This is similar to the way filter
functions should behave, in that they should not produce expected side
effects from the call since you cannot control when and where they are
called from.
Here, simply you can create the shortcode in the functions.php file and on your Page/Posts you can use the Text Module and add the [shortcode].

WP Jetpack publicize insert default text(s)

How can I place a default text (hashtag) in the Custom Message?
The textarea is (located in line 643) under jetpack/modules/publicize/ui.php
I tried to put the text in front of $title in various ways, like:
<?php echo "#myhashtag $title"; ?>
or
<?php echo '#myhashtag '.$title; ?>
but it just echoes the text, not the $title.
Any ideas will be greatly appreciated.
You can use the approach of this Wordpress plugin i made (Publicize With Hashtags), which does exactly that. It basically use and action trigger bound to the 'save_post' native event.
If you want to develop your own one you can have a look at my Source Code on the project's GitHub page or in this installation & usage guide I wrote about it.
You can add a filter, like so, to your theme's functions.php or a site-specific plugin:
add_filter( 'wpas_default_prefix', 'add_default_publicize_hashtag_prefix', 10, 4 );
function add_default_publicize_hashtag_prefix() {
$default_tags = '#yourhastaghere ';
return $default_tags;
}
This will add your default hashtag before your title without you needing to hack the WordPress core.
jetpack/modules/publicize/ui.php itself states in its comments:
/**
* Only user facing pieces of Publicize are found here.
*/
You added your hashtag in the textarea which allows admins to enter a custom message (click edit and it will slide down with your hashtag).
As #Yazmin mentioned, the best way to permanently edit the message is using a filter. The filters available are wpas_default_prefix, wpas_default_message, and wpas_default_suffix.
Personally, I had no success using these filters and I'm interested in a working solution to this issue myself.

Wordpress - How to make plugin's short code usable in text widget

i've written a plugin which shortcodes can easily be used in every post and page. As this plugin can be useful in a sidebar as well i want to make the text widget usable for my shortcodes.
When i googled this i found out that i can use the add_filter() function to ensure that, but this is only possible if i have access to the theme's functions.php. But as i am the creator of the plugin and not of the theme, this is not usable for me.
Does anybody know how i can make a shortcode which is introduced with a plugin usable in the widgets section?
Thanks!
Open your theme's function file.
Find a free spot after the opening php tag that isn't part of a function.
add this:
if (!is_admin())
{
add_filter('widget_text', 'do_shortcode', 11);
}
save the file and you should be all set.
Open your page in edit mode.
Select your page location and line where you want to add short code.
Add code here and update..

WordPress Custom Short Code help

Anyone familiar with WordPress shortcodes? I could really use a hand! I've inserted the following code into my functions.php file for the theme I'm using...
function create_slideshow_header($atts, $content = null){
return '<div class="item_heading">'.$content.'</div>';
}
add_shortcode('slideshow_heading', 'create_slideshow_header');
function create_slideshow_white_header($atts, $content = null){
return '<span id="dyn">'.$content.'</span>';
}
add_shortcode('slideshow_heading_white', 'create_slideshow_white_header');
function create_slideshow_content($atts, $content = null){
return '<div class="item_content">'.$content.'</div>';
}
add_shortcode('slideshow_content', 'create_slideshow_content');
Now, I was led to believe by several tutorials that this should allow me to insert the following into the text editor in the WP backend...
[slideshow_heading]SLIDESHOW HEADER[/slideshow_heading]
...and the SLIDESHOW HEADER text would be wrapped in the appropriate HTML.... but it's just displaying the above as regular text. I've cleared my cache, etc...
Is there something I'm doing wrong? Thanks in advance!
SOLUTION
I failed to mention that I was using the page.ly MultiEdit plugin--which uses "custom fields" to create extra editable regions. WordPress conveniently doesn't parse shortcodes in custom fields. Normally, you can create a filter for each custom field, but since this is a plugin, you can just edit the multiedit.php file, and change line 63 from
echo $GLOBALS['multiEditDisplay'][$index][0];
to
echo apply_filters('the_content',$GLOBALS['multiEditDisplay'][$index][0]);
With a little work, you can turn Wordpress into a truly amazing CMS!
I actually checked out your code, it works for me. Your usage is right.
Try adding a die() in there to see whether the method is called in your case.

Categories