WordPress Shortcode with fallback - wordpress

I'm building a simple plugin that uses the geoservices web service and what I'm trying to do is dynamically change the content on a WordPress page based on their location. I have it working somewhat but my issue is that it's returning both the location-specific text AND the default. I know it's because i'm using the shortcode instance more than once but I don't know how to change it to ONLY show the location specific content and if the location is not set or does not match the shortcode params then fall back to the default one. I don't want to add "default" as a shortcode param because it could contain HTML or something else.
Here is an example of my shortcode:
[geo city="Orlando"]555-123-6349[/geo][geo city="Raleigh"]919-999-9999[/geo][geo city="Default"]Default text here[/geo]
So based on the above, the desired result would show Orlando's phone number if the user is from Orlando or it would show Raleigh number if they are from Raleigh. Otherwise, if they are not from either of those places, it would use the default.
Here is my shortcode:
function geo_services( $atts , $content = null ) {
// Attributes
extract(shortcode_atts(array(
'city' => '',
'state' => '',
), $atts));
require_once('geoplugin.class.php');
$geoplugin = new geoPlugin();
$geoplugin->locate();
if($city === $geoplugin->city){
return $content;
} elseif ($state === $geoplugin->region){
return $content;
} elseif ($city === 'Default') {
return $content;
}
}
add_shortcode( 'geo', 'geo_services' );
And here is what is happening when I use the example shortcode above:

I believe you may be misunderstanding how shortcodes work in WP. In your example, you have added 3 shortcodes to the content. Each of those shortcodes is going to run. Not one or the other. So doing,
[geo city="Orlando"]555-123-6349[/geo][geo city="Raleigh"]919-999-9999[/geo][geo city="Default"]Default text here[/geo]
means that each of those will be called and evaluated. $geoplugin->city is always going to return the city of the user, regardless of what attributes you supplied. And since you are returning $content in all cases, it will always spit out the content that you added inside the shortcode. This is why you are seeing all 3 responses.
Instead, I would try the approach below. If your goal is to spit out content based on the city of the user, you really don't need to supply an attribute to the shortcode. See the following example:
//in your post/page content, simply use the shortcode geo
[geo]
//your function should be
function geo_services( $atts , $content = null ) {
//
require_once('geoplugin.class.php');
//
$geoplugin = new geoPlugin();
$geoplugin->locate();
//
switch( $geoplugin->city ) {
case 'Orlando':
return '555-123-6349';
break;
case 'Raleigh':
return '919-999-9999';
break;
default:
return 'Default text here';
break;
}
}
add_shortcode( 'geo', 'geo_services' );

Providing another answer based on OP comments. If you really need to manage the content via the WYSIWYG, then you could supply the content for each city as an attribute.
//add shortcode to post/page content
[geo orlando="555-123-6349" raleigh="919-999-9999" default="Custom default text here"]
//your function should be
function geo_services( $atts , $content = null ) {
//don't use extract since we expect X number of atts now
$atts = shortcode_atts(array(
'default' => 'Default text here'
), $atts);
//
require_once('geoplugin.class.php');
//
$geoplugin = new geoPlugin();
$geoplugin->locate();
//was the city provided as an attribute?
if( isset($atts[ strtolower($geoplugin->city) ]) ) {
return $atts[ strtolower($geoplugin->city) ];
}else {
return $atts['default'];
}
}
add_shortcode( 'geo', 'geo_services' );
You may have to get creative with the HTML portion of the content, but now you can include X number of cities, with their custom content, in the shortcode itself. If the city is not supplied, or does not match, it will fallback to the default.

Related

Print "custom label" for indicating type of post in the front end for Wordpress

I have a blog with different custom post types ("books", "interviews", "recipes", "events", etc...). those are all appearing in the home page with same format like a grid. I would like to print in the front end a "label" customized possibly, representing the kind/content of post.
For example:
if the post is a CPT "Book", I want to show in the grid cell "looking for a book?"
if the post is a CPT "Recipe", I want to show on same position, for specific post "hungry?"
etc...
Can you maybe help me in this? I guess I need some PHP code and set it with Elementor, but I am not a developer... :(
Thanks for any help.
Mario.
I have been asked in comment to put a screenshot. this is a fake grid taken from internet (I know, ugly layout), presenting in descending order by date all posts, very different in domain (different custom post types), which I am able to do it. What I need is, depending by Post Type, to add a slogan like "watch the movie" or "hungry?" or "Interview with...", a static string totally dependent by the type of CPT.
fake sample from internet
Further integration to explain the context.
See the current home page of my site: click here
You see two "posts" in a grid (3 columns, published with "post" widget in elementor and a custom skin. This custom skin is linked to a "loop" template created with "ele custom skin". As you see by the pic, you have one post which is a recipe (custom post type "Recipe") and one is a book (custom post type is book). But here I can eventually find also a standard post. Now, when you see the "red dot", I would like to put a word, which is directly dependent by the post type:
if "recipe" --> "Hungry?"
if "book" --> "our book reviews"...
etc...
as a sample I have in this link click here for each loop in the grid, called using "shortcode" widget
[helloworld]
and coded in snippets plugin following portion of php code
function HelloWorldShortcode() {
return '<p>Hello World!</p>';
}
add_shortcode('helloworld', 'HelloWorldShortcode');
Here is the code, from this code shortcode will be [vkh_display_post_tagline]
/**
* Custom shortcode to display the tagline by the post type.
*/
/**
* Custom shortcode to display the tagline by the post type.
*/
function vkh_display_tagline_by_post_type() {
global $post;
if ( ! $post || empty( $post ) ) {
return;
}
// Uncomment the next line for debugging and to know if we're getting post type of not.
// return $post->post_type;
$heading = '';
switch ( $post->post_type ) {
case 'recipe':
$heading = __( 'Hungry?', 'your-text-domain' );
break;
case 'book':
$heading = __( 'Our book reviews', 'your-text-domain' );
break;
// Add more cases following above examples.
default:
// If you have to assign anything default when a case doesn't match
// then write that here.
$heading = '';
break;
}
// When heading is not empty return heading within the html tag.
if ( ! empty( $heading ) ) {
return '<h4 class="post-tagline">' . esc_html( $heading ) . '</h4>';
}
}
add_shortcode( 'vkh_display_post_tagline', 'vkh_display_tagline_by_post_type' );

Gravity Forms - Accessing field defined in shortcode

I'm writing a rating system using Gravity Forms. Next to each post is a "Vote" link which opens a lightbox with a Gravity Forms shortcode in it. The shortcode populates two hidden fields - the Post ID and the User ID:
[gravityform id="2" field_values="gf_rating_post=123&gf_rating_user=456" title="false" description="false" ajax="true" tabindex="49"]
Inside the form are dropdown menus to rate the various elements, from 1-10. I'm trying to write code to insert SELECTED with the previously selected value from the dropdown, but I need to be able to reference the Post ID in order to retrieve that previously selected value.
add_filter('gform_pre_render_2', 'ac_populate_post_ratings');
add_filter('gform_pre_validation_2', 'ac_populate_post_ratings');
add_filter('gform_pre_submission_filter_2', 'ac_populate_post_ratings');
add_filter('gform_admin_pre_render_2', 'ac_populate_post_ratings');
function ac_populate_post_ratings($form) {
foreach ( $form['fields'] as &$field ) {
if ( $field->type != "select" || strpos( $field->cssClass, 'populate-posts' ) ) {
continue;
}
$choices = $field->choices;
foreach ( $choices as $choice ) {
// echo "defaultValue: " . $field->defaultValue . "<br />";
$newchoices[] = array( 'text' => $choice['text'], 'value' => $choice['value'] );
}
$field->choices = $newchoices;
}
return $form;
}
It seems that because the Post ID value is dynamically added to the form, it isn't able to be referenced using the field's defaultValue, which is always empty even though when you view the source, the input value is set correctly.
Any ideas how I can reference that Post ID inside the ac_populate_post_ratings() function?
As a last ditch effect, I contacted Gravity Forms support, and they came through with the goods.
The field defaultValue property will only contain a value if you have configured that setting in the form editor. Values which are being dynamically populated are not available from the $form.
The gform_pre_render filter is passed the dynamic population values from the shortcode in its third param.
I needed to change this line:
add_filter('gform_pre_render_2', 'ac_populate_post_ratings');`
to this:
add_filter('gform_pre_render_2', 'ac_populate_post_ratings', 10, 3 );`
And this line:
function ac_populate_post_ratings($form) {
to this:
function ac_populate_post_ratings( $form, $ajax = false, $field_values = array() ) {
And now I can access the values like so:
$gf_rating_post = rgar( $field_values, 'gf_rating_post' );

How to set a WooCommerce email template as default for all emails

I’m looking for a way to send all WordPress emails using a custom WooCommerce template so all emails will look the same.
The path to the template would be:
woocommerce/emails/my-custom-woocommerce-template.php
Does it have to all be templatized in a single file? If not, a combination of these entry points can probably get you the standardization you're looking for:
email-header.php lets you customize the start of the email including the header image (if you need to do more than change its URL). It opens the layout tags for the rest of the email content
email-footer.php lets you customize the footer, and closes the layout tags started in the header.
email-styles.php or the woocommerce_email_styles filter let you customize the CSS (see some gotchas in my article here).
Various actions/filters are scattered throughout the emails for customizing individual parts.
You can use the below function. It is working
function myplugin_woocommerce_locate_template( $template, $template_name, $template_path ) {
global $woocommerce;
// List of all templates that should be replaced with custom template
$woo_templates = array(
'emails/admin-new-order.php',
'emails/admin-failed-order.php',
'emails/admin-cancelled-order.php',
'emails/customer-completed-order.php',
'emails/customer-new-account.php',
'emails/customer-note.php',
'emails/customer-on-hold-order.php',
'emails/customer-processing-order.php',
'emails/customer-refunded-order.php',
'emails/customer-reset-password.php',
);
//Check whether template is in replacable template array
if( in_array( $template_name, $woo_templates ) ){
// Set your custom template path
$template = your_template_path.'emails/my-custom-woocommerce-template';
}
// Return what we found
return $template;
}
add_filter( 'woocommerce_locate_template', 'myplugin_woocommerce_locate_template', 10, 3 );
add_filter( 'wp_mail', 'your_wp_mail_action' ); // $args = compact( 'to', 'subject', 'message', 'headers', 'attachments' )
function your_wp_mail_action( $args ) {
global $your_prefix_your_email_args; // the args you could use in my-custom-woocommerce-template file
$your_prefix_your_email_args = $args;
ob_clean();
get_template_part( 'woocommerce/emails/my-custom-woocommerce-template' );
$args['message'] = ob_get_clean();
// ... your logic
return $args;
}
To view and update email settings, log into your website dashboard. In the left-hand menu, click on WooCommerce → Settings.
There, you’ll find several options tabs at the top. Click Emails to view the following templates
you can custom all as you want

How to disable Yoast SEO adding article:author meta tags to pages

The excellent Yoast SEO plugin is adding some unwanted meta tags.
For example, I would like article:author to appear on posts, but not on pages or other content types.
Is there a way to adjust this globally?
I'm happy to edit functions.php, but I'm just unsure what I should be hooking in to.
I would be grateful for any pointers from those more familiar with the plugin.
I tried this:
function wpseo_show_article_author_only_on_posts() {
if ( !is_single() ) {
return false;
}
}
add_filter( 'xxxxxx', 'wpseo_show_article_author_only_on_posts' );
I need to know what hook should replace xxxxxx.
You're looking for the wpseo_opengraph_author_facebook filter, which ties into the article_author_facebook() method in frontend/class-opengraph.php of the plugin.
function wpseo_show_article_author_only_on_posts( $facebook ) {
if ( ! is_single() ) {
return false;
}
return $facebook;
}
add_filter( 'wpseo_opengraph_author_facebook', 'wpseo_show_article_author_only_on_posts', 10, 1 );
The article_author_facebook() method does a check for is_singular(), which checks that we're viewing single page, post or attachment:
This conditional tag checks if a singular post is being displayed, which is the case when one of the following returns true: is_single(), is_page() or is_attachment(). If the $post_types parameter is specified, the function will additionally check if the query is for one of the post types specified.
The additional filter for ( ! is_single() ) ensures that article:author is only added to posts.
If people are looking for a way to remove more yoast SEO tags, just lookup the file wordpress-seo/frontend/class-opengraph.php, and you can see which filters you can hook into to remove certain tags.
This is the code I use to remove these tags from pages: url, image, title, description and type:
function remove_yoast_og_tags( $ogTag ){
// Do a check of which type of post you want to remove the tags from
if ( is_page() ) {
return false;
}
return $ogTag;
}
$yoastTags = array(
"url",
"image",
"title",
"desc",
"type"
);
foreach ($yoastTags as $tag) {
add_filter( 'wpseo_opengraph_'.$tag, 'remove_yoast_og_tags');
}

WordPress replace content of a page (plugin)

I'm writing a plugin that adds a page with a tag [deposit_page]; that tag should be replaced with some PHP code.
This is what I have, but it doesn't work. Is there something I'm missing or doing wrong?
function deposit_page_content($content) {
$deposit_page_content = "here will go the content that should replace the tags";//end of variable deposit_page_content
$content=str_ireplace('[deposit_page]',$deposit_page_content,$content);
return $content;
}
add_filter("the_content", "deposit_page_content");
I just noticed I gave the same variable name to the content that should replace the tag and the function itself. Could this be the problem?
WordPress has support for [square_bracket_shortcodes] built in.
See: http://codex.wordpress.org/Shortcode_API
Here is your simple example:
function deposit_page_shortcode( $atts ) {
$deposit_page_content = "here will go the content that should replace the tags";
return $deposit_page_content;
}
add_shortcode( 'deposit_page', 'deposit_page_shortcode' );
You can paste this into your active theme's functions.php file.
If you wanted attributes, like [my_shortcode foo=bar], you'd do, at the top of this function:
extract(shortcode_atts(array(
'foo' => 'default foo',
'example' => 'default example',
), $atts));
// Now you can access $foo and $example

Resources