skip to Main Content

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


  1. Chosen as BEST ANSWER

    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:

     $content = "{{CODE1}}";
    

    And it worked like a charm. Hope this helps anyone.


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

    $content = "This is normal text and this is a <a href='http://example.com'>link</a>";
    

    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:

    $content = "Some text <a href="http://example.com">link</a>";
    

    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:

    <?php ob_start(); ?>
     <a href="http://example.com">Link this</a>
     <div>This is a lot of text?</div>
     <a href='#'>Another Link</a>
     <a href="<?php echo $a_url; ?>">Another Link</a>
     <!-- you can put whatever you want here -->
    <?php
    $content = ob_get_contents();
    ob_end_clean();
    ?>
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search