Community Forums › Forums › Archived Forums › Design Tips and Tricks › Use this Genesis function with conditional statements for multisite?
Tagged: genesis_search_text, Search Form
- This topic has 7 replies, 3 voices, and was last updated 5 years, 2 months ago by ilkweb.
-
AuthorPosts
-
July 5, 2019 at 6:50 am #492136ilkwebParticipant
Hi there,
I purchased the Prop Plus package and am working with the Business Pro theme.
I added a search box to my nav bar and used the following function in functions.php to alter the placeholder text shown to site visitors:
//* Customize search form input box text add_filter( 'genesis_search_text', 'sp_search_text' ); function sp_search_text( $text ) { return esc_attr( 'Search website' ); }
My question is whether or not this function can be altered so that different placeholder text is shown on a different page and all its subpages?
Example
http://www.example.com/en/ and all subpages shows the placeholder text 'Search'.
while
http://www.example.com/fr/ and all subpages shows the placeholder text 'Chercher'
For better context, I'm creating a multi-site, and I think I might be better off putting either function in its own plugin activated for both the /en/ and /fr/ site.
Is it possible to easily create a plugin for Genesis themes instead of using the one functions file in the child theme folder?
http:///localhost/July 5, 2019 at 11:10 am #492141andytcParticipantYou could use is_tree for parent/child pages
Add this code to functions first -
// If is tree parent/child pages function is_tree($pid) { global $post; if(is_page()&&($post->post_parent==$pid||is_page($pid))) return true; else return false; };
And then find the parent page ID's you want to target and add this code to functions.php
Example -
if (is_tree(2)) { // '2' is the parent page ID - change to your ID //* Customize search form input box text add_filter( 'genesis_search_text', 'sp_search_text' ); function sp_search_text( $text ) { return esc_attr( 'Search website' ); } }
Then just add the other pages you want to target in the same way
if (is_tree(151)) { // '151' is the parent page ID - change to your ID //* Customize search form input box text add_filter( 'genesis_search_text', 'sp_search_text' ); function sp_search_text( $text ) { return esc_attr( 'Search' ); } }
https://css-tricks.com/snippets/wordpress/if-page-is-parent-or-child/
July 5, 2019 at 1:47 pm #492146ilkwebParticipantThanks so much for the reply! The pages I am going to be targeting are actually the homepages of each website in the multisite installation. So /en/ and /fr/ are not actually WordPress pages.
Even so, would this work? http://www.example.com/en/ would be the top level domain, and therefore count as the homepage.
I doubt the following would work, so I'm eliminating it as a possibility:
if (is_tree(en)) { // 'en' is the parent page ID - change to your ID //* Customize search form input box text add_filter( 'genesis_search_text', 'sp_search_text' ); function sp_search_text( $text ) { return esc_attr( 'Search' ); } }
My other option is to use the plugin approach and place my function in each site. I'm still curious to know what my options are here so I'd be interested to carry on debating it!
Thanks again for the reply.
July 5, 2019 at 4:03 pm #492148andytcParticipantNo that won’t work for you , it’s only for parent/child pages that are correctly arranged in WordPress. For example you’d have a parent page called ‘About Us’ and child pages under that, is_tree will target the parent and all the pages under it , so it’s useful for things like custom sidebars for specific areas of the site that need to appear on all the children.
July 5, 2019 at 11:29 pm #492150Brad DaltonParticipantLink to the live English and French sub pages please.
1. You could use a ternary like this :
add_filter( 'genesis_search_text', 'conditional_search_text' ); function conditional_search_text( $text ) { $fr = esc_attr( 'Chercher' ); $en = esc_attr( 'Search' ); $output = Your Condition ? $fr : $en; return $output; }
Swap out Your Condition in the above code.
2. Otherwise, you can use if else statements like this example :
add_filter( 'genesis_search_text', 'conditional_search_text' ); function conditional_search_text( $text ) { $fr = esc_attr( 'Chercher' ); $en = esc_attr( 'Search' ); if (condition) { return $fr; } else { return $en; } }
Make sure you add the correct condition which i won't know until i see your live pages.
July 6, 2019 at 3:45 am #492157ilkwebParticipantHi Brad,
Thanks for the suggestions! I'm developing in a local host, so I cannot provide links.
I'm aware of other conditions pertaining to multisite installs: https://codex.wordpress.org/Conditional_Tags#Part_of_a_Network_.28Multisite.29
July 6, 2019 at 3:59 am #492158ilkwebParticipantNo that won’t work for you , it’s only for parent/child pages that are correctly arranged in WordPress. For example you’d have a parent page called ‘About Us’ and child pages under that, is_tree will target the parent and all the pages under it , so it’s useful for things like custom sidebars for specific areas of the site that need to appear on all the children.
I wonder how functions.php will look at a multisite install though. Perhaps this could work? I'm using one functions.php file in a single child theme, and am running two websites with one WordPress multisite network.
July 6, 2019 at 4:32 am #492159ilkwebParticipantMake sure you add the correct condition which i won't know until i see your live pages.
I've found a question similar to mine in which get_current_blog_id() is cited as the solution: https://wordpress.stackexchange.com/questions/283995/how-do-i-nest-conditionals-to-identify-sub-site-other-criteria
I've identified the blog urls as follows:
Blog ID = 1 (www.example.com/) using search box text 'Search'
Blog ID = 2 (www.example.com/en/) using search box text 'Search'
Blog ID = 3 (www.example.com/fr/) using search box text 'Chercher' -
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.