Jonnie Grieve Digital Media: Blog

Home
by on 5th May, 2021 - 4:04pm (0)

Blog: R7D12: #100DaysOfCode (More Posts)

Sometimes I feel like my coding journey is akin to fighting against an overwhelming, invisible and negative force.  One where I take 1 step forward and then another 2 back in terms of meeting my objectives.  I found some more syntax highlighting plugins that work perfectly.  The only problem is they’re not suitable for use with Custom Post Types, which is what this blog is run on.

The plugins I tested were, Code Syntax Block Version 2.0.3 By Marcus Kazmierczak  and My Syntax Highlighter Version 2.56 by  Space X-Chimp

Reworking the blog to eliminate custom post types was a possibility. But not one that I relished There’s no sign that the codebase required for Gutenburg blocks to work with CPT’s is coming any time soon and I do need code syntax highlighting.

One useful thing I was able to do was that I was able to customise the single.php plugin so it pulled in the featured image for a core WordPress post or a Custom Post Type featured image depending on what post type was being used. This was done using an if statement of course but sometimes it was not needed as if the code doesn’t find the right post type for post titles and post content it simply doesn’t output the containing HTML elements.

But… I’m a stubborn so and so sometimes and while there are times I do give and move on to something else… sometimes I will not be beaten until I’ve got what I need.

/* Your code... */
<?php echo "message"; ?>

<?php 
 if('post_type' == 'blog_posts') {?>

  code....
 <?php } else { ?>

code... 
  <?php  } ?>

 

So…. back I went again at the plugin repository on WordPress and I found this….  “Code Highlighting Block” v1.2.9 by LOOS, inc.

And it finally seems to have saved my bacon.  It does exactly what I look for in a Code Syntax highlighter. It’s compatible with the version of WordPress I’m using… tick. It adds code to the content editor (it says on the classic content editor and the Gutenburg block editor but I’ve tested it on the classic editor) so I can add code on the fly.  There does seem to be one drawback in the sense that I can’t simply copy and paste the code I need in without causing issues with the text tab of the content. I have to type it out to make sure the content sticks, but at this point, I’m taking the victory and the highlighting looks good.

The code snippet above shows how I solved the problem I previously discussed where it takes the blog_posts slug discussed and isolates content based on that post type, and if it doesn’t find it, instead outputs the content based on the WordPress core.

It’s not been fun, but I think I’ve finally found a code syntax highlighting solution I can work with. The takeaway of all of this is now that if I need I need a code syntax highlighter for core WordPress posts, I should use Code Syntax Block, but if I need one for working with the Custom Post Type classic editor as it stands right now, I can use Highlighting Code Block.

Related Links

Take a look at the code syntax highlighting plugins for WordPress I’ve been using. Hopefully, they will work for you too.

  • Plugin 1 – My Syntax Highlighter: Link
  • Highlighting Code Block: Link
  • Code Syntax Block: Link

 

This post has been assigned to the following categories

    Leave a Reply

    Your email address will not be published. Required fields are marked *