Ampersand (&) Issue
-
I have a couple Custom Field where the client used an ampersand. It turns out when the value is ‘Education & Training’ the shortcode won’t work but when I change the custom value to ‘Education and Training’. Any ideas how to resolve this before I ask the client to go back and change their ampersand symbol to ‘and’?
BTW – Can imagine a ton of uses for this plugin. Thanks!
https://www.ads-software.com/plugins/custom-content-shortcode/
-
Thank you for letting me know about this issue – I hadn’t noticed it.
Hm, interesting. I tested it, but when I enter &
amp;
as a field value, it gets automatically converted to&
when I save the post. So, a field with the value “Education & Training” displays fine using the[content]
shortcode.Could you tell me what code you’re using to display the field, and also what is the output, if any?
Is it displaying like this? Education &
amp;
TrainingI am filtering courses from multiple High Schools.
This code will note work
[loop type=”programs” field=”school” value=”BGHS” field_2=”cluster” value_2=”Education & Training”]
but this code does
[loop type=”programs” field=”school” value=”BGHS” field_2=”cluster” value_2=”Finance”]
when the cluster/course does not include the & everything filters correctly.
Thoughts?
Thanks!
OK, I found that it’s necessary to convert HTML entities including ampersand (by using html_entity_decode) when querying for field values. I changed the code accordingly, and updated the plugin to version 0.5.7.
Please let me know how it works for you.
The ampersand issue is no more ?? Thank You!
Unfortunately I have discovered something new. Empty Paragraph tags are being added around the processed shortcode.
Here is my shortcode as it appears in the Content Editor
<ul> [loop type="pos" field="school" value="BGHS" field_2="cluster" value_2="Architecture & Construction"] <li><a href="[content field='url']">[content field="title"]</a></li> [/loop] </ul>
Here is an example of my rendered html
<ul> <p></p> <li><a href="https://www.website.com/dev-site/necss3/?pos=46-0000-construction-trades-general">46.0000 - Construction Trades, General</a></li> <p></p> <li><a href="https://www.website.com/dev-site/necss3/?pos=15-1301-drafting-and-design-technologytechnician-general">15.1301 - Drafting and Design Technology/Technician, General</a></li> <p></p> <li><a href="https://www.website.com/dev-site/necss3/?pos=47-0201-heating-air-conditioning-ventilation-and-refrigeration-maintenance">47.0201 - Heating, Air Conditioning, Ventilation and Refrigeration Maintenance</a></li> <p> </p></ul>
I was digging through the menus within your plugin and I may have discovered something. Under
See an overview of your site’s content structure at: Dashboard -> Content
The shortcode for [loop] has the following warning:
Warning: get_class() expects parameter 1 to be object, array given in /home/website/public_html/dev-site/necss3/wp-content/plugins/custom-content-shortcode/includes/ccs-content-overview.php on line 462
Loop_Shortcodeas well as the shortcode for [url]
Warning: get_class() expects parameter 1 to be object, string given in /home/website/public_html/dev-site/necss3/wp-content/plugins/custom-content-shortcode/includes/ccs-content-overview.php on line 464
urlShortcodeI see, I’ve had to deal with this before.. To prevent this, it’s possible to put the loop shortcode and its content in a single line; or, use the plugin Raw HTML (which I do) so the editor doesn’t add extra p and br tags, among other things. There is a parameter strip which strips all HTML tags, but you need
<li>
and<a>
.So, I added a new parameter to the [loop] shortcode, called allow – please see update to version 0.5.8. The allow parameter strips all HTML tags except for allowed ones. For example, in your code above, this may solve the empty p tags:
allow="<a><li>"
.<ul> [loop type="pos" field="school" value="BGHS" field_2="cluster" value_2="Architecture & Construction" allow="<a><li>"] <li><a href="[content field='url']">[content field="title"]</a></li> [/loop] </ul>
If you want to manually insert p and br tags, there are shortcodes [p][/p] and [br]. I’m still not sure if this is the best solution, but it gets around the issue of unwanted tags automatically added to the post content.
OK, I put in a check to make sure parameter passed to get_class() is an object. Please see version 0.5.9. That should get rid of the warnings. This was related to how I was getting the names of the functions defined for each shortcode.
PHP Version 5.3.27
Thanks for the information – I think I’ve solved it in the latest update. I appreciate your telling me about the issue.
These were two unrelated issues, but to come back to the issue of empty paragraph tags, please try the loop shortcode with
allow="<a><li>"
. That should remove all unwanted HTML tags.The issue of WordPress adding unwanted tags goes back many years, and there are plugins (raw HTML, disable wpautop, etc) and code snippets to fix it. The tags are added before shortcodes are executed, so I haven’t found an elegant solution – I hope the strip and allow parameters will be enough in most cases.
I appreciate you fast responses and I’ll have to look into that raw plugin because I am having a hell of a time with formatting.
In text view I enter the code like this
<ul>[loop type="pos" field="school" value="BGHS" field_2="cluster" value_2="Architecture & Construction" allow="<a><li>"]<li><a href="[content field='url']">[content field="title"]</a></li>[/loop]</ul>
and when I toggle between visual and text it turns into this
<ul> <ul>[loop type="pos" field="school" value="BGHS" field_2="cluster" value_2="Architecture & Construction" allow="</ul> </ul> <ul> <li><a>"]</a></li> <li><a href="[content field='url']">[content field="title"]</a></li> <li>[/loop]</li> </ul>
I was curious what did you do to the plugin to get passed the original ampersand issue that I first posted about? I wrote a shortcode and it worked fine but I could not get passed the ampersand issue and when I was researching a solution is when I found your plugin.
<?php /** * POS Short Code **/ add_shortcode( 'pos_list', 'pos_listings' ); function pos_listings($atts, $content = null) { extract(shortcode_atts( array( 'school' => '', 'cluster' => '', ), $atts )); $args = array( 'post_type' => 'programs', 'posts_per_page' => -1, 'orderby'=> 'title', 'order' => 'ASC', 'meta_query' => array( 'relation' => 'AND', array( 'key' => 'school', 'value' => $school, 'compare' => '=' ), array( 'key' => 'cluster', 'value' => $cluster, 'compare' => '=' ) ) ); $the_query = new WP_Query( $args ); ?> <?php if( $the_query->have_posts() ): ?> <ul> <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <li> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </li> <?php endwhile; ?> </ul> <?php endif; ?> <?php wp_reset_postdata(); } ?>
Yes, toggling between visual and text editor is a nightmare. I use the plugin Raw HTML – and I also avoid using the visual editor if there’s any HTML in the text.
In fact, I have another feature in my plugin which may solve the issue. If you create a custom field called html – as a text area or editor – it will be loaded instead of the post content (with visual editor). So, you can keep all HTML code outside of the visual editor, and load the post content using the
[content]
shortcode.Depending on how extensively you’re using HTML and shortcodes, it could be a solution. There’s even one step further, which is to use the html field to load template parts or files written in HTML/PHP/shortcodes.
Or another way: keep the code for post list in a custom field (text area) and load it into the post content. That might make more sense.
The key was to use
html_entity_decode
on the values passed in themeta_query
array. Perhaps like this:'value' => html_entity_decode($cluster),
Just wanted to say thank you. Your ‘html_entity_decode’ info and some output buffering allowed me to finish writing my shortcode.
I’m going to keep my eye on your plugin for future use.
Great, I’m glad that bit of info was useful. Thanks for letting me know about these issues – it helped me to improve the plugin: fixed the ampersand, shortcode list, and added a new parameter! ??
- The topic ‘Ampersand (&) Issue’ is closed to new replies.