An Option for dealing with CORS

As we do more things with JSON, I run into CORS access issues. They can be solved in various ways but it’s often a hassle. It often irritates me as I build demos in COde

After wanting a simple solution this AM, I made this tiny, insignificant PHP file that I think might be useful to others.

Name this file cors.php.1

Put this file on a server.

You can now append some JSON URL to that URL like so … https://yourserver.com/cors.php?url=https://someurl.com/data.json and it returns drama-free JSON for your use and enjoyment. Assuming you have HTTPs on your server it should also deal with HTTP/HTTPS conflicts as well which is very handy at times.

<?php 
header("Access-Control-Allow-Origin: *");//let the people in
header("content-type: application/json");//make the return all JSON-Y
$url = htmlspecialchars($_GET["url"]);//get the URL parameter from the . . . URL
echo file_get_contents($url);//spit it back out


1 Or name it whatever you want. I’m going using bossiness as a shortcut to clarity.

Filter WordPress Title but Only in Admin Area

I am playing around with unfurling1 open graph data in the WordPress TinyMCE editor. You can see what that looks like in the video above. It’s a lot like the oEmbed experience in WordPress but with the open graph information driving it.

I want the data to go into the post body but I don’t want a title that links to a post because these types of posts are more about getting people out to that original content. People could still manually enter a title and that’d work but if they choose not to I thought it’d be nice if it was just removed from the equation.

That’s pretty nice on the front end but on the backend you end up with a maze of posts with the title (no title). That seemed irritating so I cobbled together the following function which creates a mini excerpt from the post content but only applies the filter when you’re in the backend/dashboard. Nothing fancy but a nice little pattern that I expect I’ll use again.

add_filter('the_title', 'new_short_admin_title', 10, 2);
function new_short_admin_title($title, $id) {
	if ( is_admin() ) {
	    if ($title === null || $title == '' || $title == '(no title)'){
	    	$title = super_short_excerpt();
		}
	}
    return $title;
}

function super_short_excerpt() {
    return wp_trim_words(get_the_excerpt(), 5);
}

via GIPHY


1 I like how furl and url have similar sounds but it is a ridiculous term.