I want to add HTML code into my child theme’s function.php file to create default posts in WordPress. I tried this fantastic solution which did the trick for standard text. I could add standard text and even simple HTML bold and italic tags for the text. Works great.
add_filter( 'default_content', 'my_editor_content' );
function my_editor_content( $content ) {
$content = "default content goes here....";
return $content;
}
However, for the content to add within the quotes for ($content = "default content goes here….";) I would like to add HTML text with multiple hyperlinks, and they have quotation marks which break the function of this PHP. Multiple hyperlinks won’t work due to the quotation marks needed for the code.
Is there any way to get around this to allow for some HTML with multiple hyperlinks to be added here inside functions.php for the default posts in WordPress? Thanks.
2
Answers
Ok, I actually figured this out on my own. Just wanted to share my answer in case it helps anyone.
I downloaded a plugin called Code Embed. You can then insert any code you wish as HTML, and tag it as "{{CODE1}}" for example.
So I did that and simply changed the code from the functions.php file to:
And it worked like a charm. Hope this helps anyone.
You don’t need a plugin to achive that, there is a few ways to solve it (that I know of), probably there is more:
You can use single quotes and double quotes nested, so, to place an anchor tag inside a string, you can do it this way:
The second thing you can do is scaping the special characters with (backslash), so if you want to put anchor tags into the content variable, just do this:
A bit more complicated way of achieving what you want, but cleaner for big strings with a lot of HTML elements, is using
ob_start
in this way:With this last case, you will have all your HTML markup as a string inside the
$content
variable.And last but not least, you can also use heredoc and nowdoc: it is better explained in the links I left that what I can explain about it.