0

I was following a tutorial on youtube and made a post-format page called content-link which uses the post format type of link. The template just contains the following code:

<a href="<?php get_the_content(); ?>"><?php echo the_title(); ?></a>

The post just has the Title and the content just has a link in it. The Gutenberg puts the link between two comment tags for WP paragraph was causes the link be list with the html version of the comment tags with the link between them so the link won't work. Yet if I remove the comment tags which activates the classic editor, the link works. My question is, is there away to make the above code work with the Gutenberg editor or will it only work with the classic editor?

Here is a link to the course that I was following. https://www.youtube.com/watch?v=jrBuH8JOiYM&list=PLzWy6s_olo5FmMv_EANv_1MV7i7vOMEiA&index=2&t=0s

Thank you

0
0

There are a number of problems here, lets go through them one by one

1. Functions that return stuff, and Functions that output stuff

get_the_content doesn't output anything, it returns it. So you need to echo it for the results to be visible:

echo get_the_content();

Functions that start with get_ almost always return a value.

Functions that start with the_ almost always output a value, e.g:

the_content();

Which brings us to the next issue:

echo the_title();

This is the same as:

echo '';
the_title();

Functions that return stuff need to either have echo or they should be assigned to a variable.

This gives us a correct version of what you have that works the same, but minus bugs:

<a href="<?php echo get_the_content(); ?>"><?php the_title(); ?></a>

It still has issues though

2. Security Security Security Security

What happens if your content doesn't contain a link? What if the user put something malicious and nasty in there, or made a typo?

If we apply escaping, we can guarantee it will be a URL:

<a href="<?php echo esc_url( get_the_content() ); ?>"><?php the_title(); ?></a>

It might be a weird garbled URL if malicious stuff got put in there, but it will always be a URL, 100%, guaranteed. That assumption is enforced, and it no longer becomes a matter of "it should be a URL", or "I doubt it'll ever be an issue", instead we now have a cast iron guarantee that it is always a URL

Escaping is one of the biggest security improvements you can make, and fixes a huge number of security issues. Despite this, it's almost always left out of tutorials, ignored by theme developers, and missing from guides themes and products. Escape late, escape often, it'll be a huge boost to your codes security.

3. OEmbed, Gutenberg and Post Formats

What if that URL was an OEmbed value? E.g. a Youtube Video? The editor tries to convert that into a player embed automatically, as will the_content(), but get_the_content() won't.

As a result, we're going to need to swap that out, and an <a> element won't do. Instead, lets simplify this further:

the_content();

For a link post, just use a normal block with a link in it.

4. The Future of Post Formats

Post formats are kept for backwards compatibility, in the future post formats don't make much sense in a block based content world.

E.g. it would be easier to just output a block with a link in it for link posts. Video posts would just be posts that have just a video block, etc

That's the paradigm you should be aiming for, and that's the current thinking of those working on the editor. Post Formats were never really that successful


As useful as youtube guides are, like all articles they become out of date, don't cover everything, and should be taken with a grain of salt. Diversify your research, explore alternative ways to do things, and continue to grow.

That video is just that persons idea of how to do things at that time, but it isn't the only method, or the best. In the future, block based layouts and custom blocks are going to be more useful and flexible than custom page templates, and other older ways of doing things

1
  • Thank you for your reply. First, i did you use the echo command in my original code but somehow it went missing before I pasted the code here. I had also tried the esc_url but I was still having issues so I removed it until I could figure out how to remove the p tags that were being added to the links. I was trying different searches my answer. The problem is trying to figure out how to explain to search engine what I am tryin to find. I finally came up with using the strip_tags(get_the_content) as a way to fix the problem. All of this was just being done on my computer. Sep 23 '19 at 4:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.