Add WP function to page

Hi - I need to add the WP function: <?php get_the_author_posts_link(); ?>
in the edit window of a page, so the “by [author-name]” will display on the page.

I can do it with the plugin “Insert PHP Code Snippet” from xyzscripts.com – where you enter the php and it returns a short code to paste into edit window.

BUT I’d prefer to add it to functions.php, rather than use a plugin. I believe it would look something like this:

function by_author() {
<?php get_the_author_posts_link(); ?>
}

But then I don’t know how to call up that function in the page edit window. Do I add some kind of action command to the function?

Or is the plugin converting php to shortcode the only option?

Typically this is what shortcodes are for. You can put your shortcode in the functions.php file and then put the shortcode into the page. While the Insert PHP code snippet allows you to put PHP into a page/post, you really shouldn’t put PHP directly into a page if you can help it. Ideally the way you do this is through a shortcode. That way you don’t have PHP in your posts that run arbitrary code.

Here is a nice article to get you started…

I hope this helps. :slight_smile:

Hi - thanks for reply! I tried this:

function by_author2_shortcode() {
the_author_posts_link();
add_shortcode('by-author2', 'by_author2_shortcode');
}

but that displays on the wordpress page the words: [“by-author2”] when I enter that shortcode, whereas I want it to to display the function (viz. my name hyperlinked to all my posts).

Do you know how to write the php? Or do I need to go to php forum? I don’t know how to write php.

I also tried this but same result, just displays [“author2”]

function author2_shortcode() {
    return the_author_posts_link();
}
add_shortcode( 'author2', 'author2_shortcode' );

Ok this second version is what you want to be using in your functions.php, not the first one you show. Then in the post you enter [author2]. Notice no double quotes. Try that and see if it works for you.

Wow that works! Thanks million! Now my only problem is how to get a shortcode into a paragraph so it right-aligns?

I tried this:

<p class="alignright">by [author]</p>

but it displays author at default left and “by” on the right :slight_smile:

There’s no option in meta settings to give a shortcode block an alignment

I’m guessing the “by” before the author name and the right-align both need to be in the php? but I don’t know how to do that…

I figured out the by:

function author_shortcode() {
	$name = 'by ';
	echo 'by ';
    return the_author_posts_link();
}
add_shortcode( 'author', 'author_shortcode' );

so now my only problem is how to code rightalign into the php so it will display as {text-align:right;}

I finally settled on this and browser assumes a closing </p>

function author_shortcode() {
echo "<p style='text-align:right'>";
	$name = 'by ';
	echo 'by ';
	return the_author_posts_link();
}
add_shortcode( 'author', 'author_shortcode' );

Big problem. Wordpress complains every time: “Updating failed. The response is not a valid JSON response.”

It updates the page – even though it says it failed. The problem is that [author] shortcode block. If I delete it, then wordpress does not give that JSON error message.

Is this normal in gutenberg? To update a page it says it’s not updating because of a shortcode block that’s derived from functions.php? I can’t see an error in the php other than there’s no closing </p>