Skip Navigation

[Résolu] convert state abbreviations to full state name in a shortcode from url parameter

This support ticket is created Il y a 8 années et 5 mois. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 6 réponses, has 2 voix.

Last updated by kelliS Il y a 8 années et 5 mois.

Assigned support staff: Ghennadi.

Auteur
Publications
#191440

I am using a parametric search with a select field using custom field States. And then above the search results I am displaying the url parameter that is being filtered. hidden link For example...

Results: AK (this is displayed using a shortcode function that pulls a url param that I found on another post)

Some Company
Anchorage, AK

Another company
Juneau, AK

What I want to do is add some sort of array conversion function so that the abbreviation in the Results: AK says Results: Alaska (or whatever the abbreviated state is). I found a php conversion function that starts like this but wasn't sure where or how to apply something similar to my shortcode that is pulling the url param and displaying it in the view.


function convertState($state) {
$stateList = array(
"AL" => "Alabama",
"AK" => "Alaska",
"AZ" => "Arizona"
etc etc

Thanks for your help in advance!

#191756

Dear kelliS ,

You can try something like this

add_shortcode('country_code_convertor', 'country_code_convertor_func');
function country_code_convertor_func( ){
	extract( shortcode_atts( array(
              'state' => ''
		   ), $atts ) );
	$stateList = array(
		"AL" => "Alabama",
		"AK" => "Alaska",
		"AZ" => "Arizona"
		);
	
	if ( !empty($state) ){
		if ( isset($stateList[$state]) ){
			// return $stateList[$state].', '.$state;
			return $stateList[$state];
		}
	}
}

Usage: [country_code_convertor state="STATE2CHARSCODE"]

Note. Don't forget add your custom shortocode to list of allowed shortocdes in Views>Settings>Third-party shortcode arguments

Please let me know if you are satisfied with my answer and if I can help you with any other questions you might have.
Regards,
Gen.

#191964

Thank you for the quick response. So I put your code into my functions file and then added the shortcode to the Views settings page under 3rd party shortcodes. Where there might be confusion is that in your shortcode I can define state=" ". But I'm already using another shortcode to grab the state parameter from the url passed and display it. I'm using this code and shortcode in the View.

add_shortcode('wpv-post-param', 'wpv_post_param_shortcode');
function wpv_post_param_shortcode($atts) {
if (!empty($atts['var'])) {
$out = $_REQUEST[$atts['var']];
if (is_array($out)) {
return implode(',', $out);
} else {
return convertState($out);
}
}
}

And then using this shortcode [wpv-post-param var="state"] to display the state they selected, in the View above the results. That was working fine, but instead of showing AK which is what's saved in the database and passed in the url string, I wanted to convert it and show Alaska, etc.

So I tried using both the original function and shortcode, along with your function and shortcode in my View - like this:
[country_code_convertor state="[wpv-post-param var="state"]"]

But maybe it's not possible to use a shortcode within a shortcode? Didn't show anything or work when I had that in place.

Thanks again. I appreciate your help!

#192223

Dear kelliS ,

You no need use 2 shortcodes for this.

add_shortcode('wpv-post-param', 'wpv_post_param_shortcode');
function wpv_post_param_shortcode($atts) {
	if (!empty($atts['var'])) {
		$out = $_REQUEST[$atts['var']];
		if (is_array($out)) {
			return implode(',', $out);
		} else {
			return convertStateToName($out);
		}
	}
}
function convertStateToName( $state = ''){
	$stateList = array(
        "AL" => "Alabama",
        "AK" => "Alaska",
        "AZ" => "Arizona"
        );
	if ( !empty($state) ){
		if ( isset($stateList[$state]) ){
            return $stateList[$state].', '.$state;
            //return $stateList[$state];
        }
	}
	return $state;
}

Usage: [wpv-post-param var="state"]

Please let me know if you are satisfied with my answer and if I can help you with any other questions you might have.
Regards,
Gen.

#192341

I removed all the old functions and replaced with this new code (in my themes functions.php file), and it does still work to pull the url parameter and display it in the Results: area. However, it's still not converting it to the full name state. I also made sure that I registered that shortcode in the settings area.

Here is the page to see: hidden link

Thank you so much for your continued help! I appreciate it.

#192579

Dear kelliS ,

Can you try this?

add_shortcode('wpv-post-param', 'wpv_post_param_shortcode');
function wpv_post_param_shortcode($atts) {
    if (!empty($atts['var'])) {
        $out = $_REQUEST[$atts['var']];
        if (is_array($out)) {
            return convertStateToName($out[0]);
        } else {
            return convertStateToName($out);
        }
    }
}
function convertStateToName( $state = ''){
    $stateList = array(
        "AL" => "Alabama",
        "AK" => "Alaska",
        "AZ" => "Arizona"
        );
    if ( !empty($state) ){
        if ( isset($stateList[$state]) ){
            return $stateList[$state].', '.$state;
            //return $stateList[$state];
        }
    }
    return $state;
}

Usage: [wpv-post-param var="state"]

Please let me know if you are satisfied with my answer and if I can help you with any other questions you might have.
Regards,
Gen.

#192906

That did work! Yay! 🙂 It did make it say Alaska, AK - so I just removed this little part from line 20 and then it just said Alaska:

.', '.$state

Thank you again! Many people have praised your support forum, and I agree with them. You have been very helpful!