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.
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.
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.
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>