Per Page custom code

From old feedback: Per Page custom code | Cwicly

I like the ability, in Bricks, to add custom code (CSS, JS) on a page basis. It’s super convenient

Important to properly run custom code inside <head> and before closing <body> per page basis from FSE environment.

I think that Global Parts → Fragments are the way to go with this.
Our end goal is to have Template Parts, Stylesheets and JS available to add and condition in Global Fragments.

Interesting. The approach seems to be very smart, thanks for elaborating.
Gonna wait and see how it turns out.

What would be the best way to insert code in the head or before closing body tag?
Is this currently possible with Cwicly on a individual post/page basis inside the FSE environment?

Code block → wp_head and wp_footer :wink:

1 Like

Thanks for hinting @Louis, gonna check it out :+1:

For anyone interested:

<?php
add_action("wp_head", function () {
    echo '<script id="cwicly-header">console.log("Hello there, this is coming from the header")</script>';
});
add_action("wp_footer", function () {
    echo '<script id="cwicly-footer">console.log("Hello there from the footer")</script>';
});
?>

From the code block, you can apply any Cwicly condition like for any other block. That should do the trick!

1 Like

I am revisiting this topic. We are currently implementing hardcoded schema for every page/post. Now with another large site we have done, we managed this with another builder that had the option for custom code per page. This was ideal because our SEO manager could paste it in there without issue. Now the code block calling a PHP function is doable, it is not ideal. I will have to implement the custom schema script for him on every single page/post.

Possibly consider adding a ‘Custom Code’ section in the page settings of every page/post similar to what you have in Cwicly Settings. Also maybe even an SEO section. This would be very helpful I believe for other tasks as well.

Something like this is probably a good candidate for a code block in your page template (or a snippet that runs on page load) and some custom fields to determine the specific per page parameters.

Alternatively, you can potentially just use an SEO plugin or dedicated Schema plugin for this, to save you having to do the custom code in the first place.

@StrangeTech proper schema must be hardcoded specific to the page for best results really. You can get a plugin but most auto generate it with less than desirable results. Bricks has the option for Custom Code per page/post and it is a popular feature. It also has many uses besides SEO. I usually create a function in functions.php to load a particular script on a page which works if its a few pages. But any more than that, then I have to find another solution. I hope at some point it can be reconsidered.

Hi @hopscotch,

Unless I am not understanding your intent, this is very much supported by SEO plugins like Rank Math:

This not only allows you to customise the Schema on a per-page and per-post basis but also allows you to create templates.

In addition Rank Math has filters that allow you to further customise the output of Rich Snippets in an automated way:

We have used this for example to make the page author always be a single person:

add_filter( 'rank_math/json_ld', function( $data, $jsonld ) {
	$user = get_user_by('login', 'Joe Blogs');
	$author_name = $user->display_name;
	$author_description = RankMath\Helper::get_user_meta( 'description', $user->ID );
	$author_url = esc_url( get_author_posts_url($user->ID));
	$author_image = get_avatar_url($user->ID);
	
    if( isset($data['ProfilePage']) ) {
		$data['ProfilePage']['@id'] = $author_url;
        $data['ProfilePage']['name'] = $author_name;
        $data['ProfilePage']['url'] = $author_url;
		if ($author_description) {
			$data['ProfilePage']['description'] = $author_description;
		} else {
			unset($data['ProfilePage']['description']);
		}
		if ($author_image) {
			$data['ProfilePage']['image']['@id'] = $author_image;
			$data['ProfilePage']['image']['url'] = $author_image;
		} else {
			unset($data['ProfilePage']['image']);
		}
    }
    if( isset($data['WebPage']) ) {
		$data['WebPage']['author']['@id'] = $author_url;
    }
	/*if( isset($data['BlogPosting']) ) {
		$data['BlogPosting']['author']['@id'] = $author_url;
    }
    if( isset($data['Article']) ) {
		$data['Article']['author']['@id'] = $author_url;
    }*/
	
    return $data;
}, 99, 2);
add_filter( 'rank_math/snippet/rich_snippet_article_entity', function($entity) {
	$user = get_user_by('login', 'Joe Blogs');
	$author_url = esc_url( get_author_posts_url($user->ID));
	
    if($entity['author']){
        $entity['author']['@id'] = $author_url;
    }

    return $entity;
});

This is not to say that per page/post code is not a valid feature request, I am just pointing out that for this use-case you may get a more user friendly editing experience with a plugin.

This looks promising! But our SEO manager prefers Yoast and you have to do it there way with schema. I will discuss it with himself. Thank you for the info :blush:

1 Like

Understood. This is one of the many reasons we use Rank Math.

Yoast does have some filters for customisation, but it does require more customisation: