In the Genesis Facebook group a user asks:
Hi all–I am modifying a genesis sample child theme. I am looking for a way to modify the site title URL so that depending on what header image is displayed depends on what URL is linked to that header image. I’m using the WP Display Header plugin that allows me to determine which header image is displayed on a page, but that does not change the underlying site URL. The reason for this is that I have a site area that will be restricted and I would like it if the special header for that restricted area would go to that area’s central page, rather than the unrestricted home page, if that makes sense. I’ve found some PHP code that might do the trick, but I don’t want to change the site URL globally, just on a per page basis.
We can create a URL type custom field using Custom Field Suite plugin, set it to appear when editing Pages and when a custom URL has been entered, link the site title element (.site-title a) of that particular Page to the specified URL using the genesis_seo_title
filter hook in Genesis.
Backend:
Frontend:
Pages for which no custom site title URL has been set and other pages of the site will continue to link the site title to its default, the site’s permalink.
Step 1
Install and activate Custom Field Suite.
Step 2
Go to All Field Groups and create a custom field group having a Site Title Link
field (set the return type i.e., Output format to PHP array).
Step 3
Edit your Page(s) and enter your custom URLs for the site title.
Step 4
Add the following in child theme’s functions.php:
add_filter( 'genesis_seo_title', 'custom_site_title_link', 10, 3 );
/**
* Set Site Title hyperlink to that of `site_title_link` custom field on Pages.
* @param string $title Existing page title.
* @param string $inside Markup inside the title.
* @param string $wrap Wrapping element for the title.
* @return string Existing / Modified title.
*/
function custom_site_title_link( $title, $inside, $wrap ) {
// if this is not a static Page, abort.
if ( ! is_page() ) {
return $title;
}
// get the array of input values for `site_title_link` custom field.
$link = CFS()->get( 'site_title_link' );
// if a custom Site Title Link has been set for this Page, set the site title link to it.
if ( $link['url'] ) {
$inside = sprintf( '<a href="%s">%s</a>', $link['url'], get_bloginfo( 'name' ) );
// Build the title.
$title = genesis_markup( array(
'open' => sprintf( "<{$wrap} %s>", genesis_attr( 'site-title' ) ),
'close' => "</{$wrap}>",
'content' => $inside,
'context' => 'site-title',
'echo' => false,
'params' => array(
'wrap' => $wrap,
),
) );
}
// return the current title or title with modified hyperlink.
return $title;
}
References:
genesis/lib/structure/header.php
How does Custom Field Suite compare to ACF or CMB2 on the front end.
CMB2 doesn’t run on the front end so it’s got to be the lightest though it requires more code to access the postmeta data.
I do not find any JS or CSS being added on the frontend by CFS.
With ACF, as you are probably aware we can disable it on the frontend. https://www.billerickson.net/code/disable-acf-frontend/
Another one is Carbon Fields. Bill Erickson switched to it from ACF.
Yeah, I saw Bill’s post.
I experimented with using CMB2 instead of ACF and it was much faster: https://www.damiencarbery.com/2017/10/changing-from-acf-pro-to-cmb2/
I’ll give Custom Fields Suite and Carbon Fields a try.
Hi Sridhar, this is just what I’ve been looking for, except that I need to stick with ACF. Can this be modified to work with ACF? Also, it will need to be applied to Header Image .site-title if that makes any difference. Thanks!