A Shortcode for Creating Link to a Post or Page

Last Updated on April 3, 2020 by Neil Murray

This site uses many internal links to relate to other pages. It is very easy to create a post/page link to other page using WordPress editor, but once we modify the page slug, move the page to other hierarchy, or move the site completely with different domain, we will got 404 not found page.

For a workaround for this issue, we should avoid static link to a post or page, and use a shortcode that generates the link automatically based on post ID. We can find the ID of post or page in respected admin edit screen as screenshot above from https://team.cf7skins.com/wp-admin/edit.php?post_type=page for Page type.

Creating Shortcode Link

Creating a shortcode in WordPress is easy, we just need to register our shortcode function using add_shortcode and shortcode_atts to combind user shortcode attributes with default shortcode attributes.

Here is how the code looks like:

function cf7skins_link( $atts ) {
	$atts = shortcode_atts( array(
		'id' => 701,
		'text' => '',
	), $atts, 'link' );
	
	extract( $atts, EXTR_SKIP ); // extract attributes
	
	if ( $permalink = get_permalink( $id ) ) { // return string if found or false.
		$title = $text ? $text : get_the_title( $id );
		return "<a href='$permalink'>$title</a>";
	} else {
		return 'Error, the post or page does not exist!';
	}
}

add_shortcode( 'link', 'cf7skins_link' );

The shortcode takes two attributes or parameters, id for the post ID and title for custom post/page title. If title is not set, than it will use the post title itself. The function will finds the post ID, create the link if found, or display error message if not exist.

To use the shortcode, simply add a square bracket with link string, provide the post ID and custom title if needed.

[link id="144"]

That’s it, with a few lines, we can add post/page link automatically, and save the site from 404 suffering.

Working with Regex

Last Updated on April 18, 2020 by Neil Murray

In some of our plugins use PHP and JavaScript regular expression or regex to find and replace string or do validation on strings. In PHP, we can write the code and reload the page to see the changes, which takes more steps to see the results. The same as testing JavaScript regex in a web page, that needs to reload the page. In JavaScript we can use browser console directly but I found have difficulties to write script using it, and we will get error if there are repeated var, const or let.

Both testing above can be a pain and takes time to develop. I want a tool that can display the result immediately without reloading the page, have a test string, notice us for any errors, match information, explanation for each regex, and have a good documentation.

The tool should be a web page, no need to install or registering process to use it. So I Google it and found https://regex101.com/ that has features I need. With that, I can write the regex, put the test string and see the results easily.

For example in Visual, after saveXML, PHP DOM create two spaces for indent/tab, while we want a four spaces tab. Regex can help to find all matched strings and replace them with tab as seen in screenshot below.

It’s very easy to work with regex using the tool. You should try it, or find another tool that suit your needs. Happy writing!