0

I have one php file in wordpress where have a php variable $tt->name.

This $tt->name I want to convert into a shortcode so I canuse it somewhere else on the website.

Let me explain step by step. First I have search where user choose taxonomy and then type in search box and click on search.

On search result page, taxonomy is as $tt->name (Because there are 3-4 types of result, depend on what user choose, like if 1st taxonomy then another result and if 2nd taxonomy then a message, if 3rd taxonomy then a pic and so on..

But all on the top have line, "You have chosen taxonomy 1" and that 1 is $tt->name in code actually. Now there is a form just in next with title, contact us and there is a field which I want value of $tt->name. That's why I need shortcode. Because I want to use this shortcode somewhere else in the website.

Any solution for this?

Thanks

  • 3
    What is $tt->name and where does it come from? Can you describe what the purpose of the shortcode is in non-technical terms? There's not a lot of context here to understand what's going on – Tom J Nowell Jun 28 '18 at 16:36
  • Hello, thanks for your reply. It's actually on search result page. This is taxonomy. Like all countries, and which country user choose, it show at the place of $tt->name. But on same page, there is form also and form use shortcode. So to put in that form, I need shortcode for this... – djmhim Jun 28 '18 at 16:40
  • Is this a shortcode ran using do_shortcode? Or do you have a custom search page template? – Tom J Nowell Jun 28 '18 at 16:56
  • No no, it's not shortcode at all.. It's just a custom search page template. – djmhim Jun 28 '18 at 16:59
  • 1
    I meant for the form, there's no code in your question so it's very difficult to figure out what's going on and how to change it, are you aware that you can modify the normal search functionality via search.php for the template and pre_get_posts to modify what gets searched? – Tom J Nowell Jun 28 '18 at 17:33
0

It sound like you can accomplish this with 2 PHP functions.

1) Save the searched taxonomy to the user's session. When they re-run the search function, update the taxonomy that was searched. This way if its saved to their session, you can use it elsewhere on the site just for that user.

2) Create a shortcode to display that saved taxonomy.

// ... search function that gets $tt. 
save_searched_taxonomy($tt->name);
//...

function save_searched_taxonomy($taxonomy){
    $_SESSION['searched_taxonomy'] == $taxonomy;
}


function get_searched_taxonomy_shortcode(){
    return $_SESSION['saved_taxonomy'];
}
add_shortcode('searched_taxonomy', 'get_searched_taxonomy_shortcode');

Then on other pages, you can call your shortcode and have it refer to whatever that user searched for.

[searched_taxonomy]
  • Hello italiansoda. Thanks a lot for your help. But this one didn't work for me ;( – djmhim Jul 1 '18 at 13:29
  • Are you getting any errors? Also here is more about session. w3schools.com/php/php_sessions.asp Maybe you need to do session_start(); at the top of your code somewhere. – italiansoda Jul 1 '18 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.