Displaying code snippets in WordPress - wordpress

Ok, not technically a programming question but I want a plugin for my WordPress blog that displays code snippets.
Can anyone here recommend a good one? Also, do you guys know which one Jeff uses on codinghorror. That one looks really good.
Thanks.

wordpress.com claims to use Alex Gorbatchev’s syntaxhighlighter Google Code project.

I guess you might be interested in run-this - there is a wordpress plugin to allow your readers to run your snippets in the browser.

For WP-Snippets, I use Prettify.
You should probably use a plugin for escaping characters to be able to display code without having to do it yourself, or your code wont be displayed. Or just put this in functions.php in your theme folder to do this automatically:
function bbcode( $attr, $content = null ) {
$content = clean_pre($content); // Clean pre-tags
return '<pre"><code>' .
str_replace('<', '<', $content) . // Escape < chars
'</code></pre>';
}
add_shortcode('code', 'bbcode');
And then wrap your code with [code]<?php echo 'hello world!'; ?>[/code]
Source: Code in posts

Related

Seo yoast plugin, breadcrumbs

This is question on the seo yoast wordporess plugin.
I have a friend who doesn't know that much about code/wordpress/etc, they have an ecommerce site built using magento and a blog using wordpress which is styled to match the main site and they use yoast seo plugin for seo etc. They have asked me to try an get the breadcrumbs working for them in a different way to what i know, from what i can see they want to add a prefix to the breadcrumbs 'home' which links to main site then renaming the blog from home to blog, for example home(main site) > blog(blog) > post.
Now i did just that in the plugin settings but they said that the schema markup wouldn't be complete and said there’s a filter hook called ‘wpseo_breadcrumb_links’ which gives access to the array of URLs and anchor text used to build the breadcrumbs, now i cant find anything on google that explains this or how to start writing it, i do know that it needs to go in the functions file.
Would it be possible to get some help on this.
Thanks in advance and very much appreciated.
Justin
I'm not sure what Schema.org markup has to do with breadcrumbs! :) Schema is used on post/page level. So, they are either oversmarting themselves or I got you wrong.
I think this sample code may be helpful:
add_filter( 'wpseo_breadcrumb_output', 'custom_wpseo_breadcrumb_output' );
function custom_wpseo_breadcrumb_output( $output ){
if( is_product() ){
$from = '<span typeof="v:Breadcrumb">Products</span> »';
$to = '';
$output = str_replace( $from, $to, $output );
}
return $output;
}
It looks like wpseo_breadcrumb_output gives you access to the entire output instead of just the link portion. Please see this page for more details: http://wpquestions.com/question/showChrono/id/8603

Wordpress adding custom fields into post's front end

i want to make a wordpress plugin that will display some content at the end of each post. let's say for example 'Hello world'.
But looking at the wordpress API, i don't find any relevant indications about how to do this.
i don't want a widget, but only a plugin that does only that: displays me a static text at the end of every article.
any idea about how this should be done?
thanks!
See if this forum post helps you:
http://wordpress.org/support/topic/simple-wordpress-plugin-to-add-actionable-text-after-blog-content-feedback?replies=1
The lines doing the magic are:
...
add_filter('the_content','now_what_pjrvs');
...
function now_what_pjrvs($content = '') {
return $content . $text;
}
Where $text is the code that you want to add at the end of the post content.
Hope it helps.
Tell me if you need more references.

WordPress plugins - don't print the comments on a post page

I'm making a plugin where I want the comments in a single post page not to be printed at all. I need to query the database myself and print my own html with the results.
How can I make WordPress to not print the comments, without disabling them?
Thank you
EDIT:
as a suggestion, I am using:
apply_filters('comments_template', array($this,'comments_template'), 10, 1);
function comments_template($template){
$template = '';
return $template;
}
nothing happens, what am I doing wrong?
You could use the comments_template filter to make WordPress use your plugin's template file rather than the current theme's.
EDIT: based on your edited code: unfortunately you need to have an actual file, the path to which you return in $this->comments_template()...
class MyPlugin{
//add the filter somewhere...
function comments_template($template){
return dirname(__FILE__) . "/my_comments_template.php";
}
}
The file plugin_dir/my_comments_template.php must exist, otherwise WP falls back on the default theme's comments.php. See wp-includes/comment-template.php on lines 911-917.
In plugin_dir/my_comments_template.php you could call `MyPlugin::do_comments() or something like that. I don't know any other way around this. Let me know if you find a better way.
Cheers, Chris

switch wordpress theme base on URL

I have a wordpress website that host several themes inside it.
Now I want to switch the themes for visitor base on the URL that they enter.
for example :
my WP website is at http://www.myblog.com/wp/
but if user enter :
http://www.myblog.com/wp?theme=twentyten -> using twentyten theme
and if user enter :
http://www.myblog.com/wp?theme=mycustomtheme ->using mycustometheme
then
is there any plugins available out there? I've been searching for days but no one seam to work for me.
Any suggestion is very appreciated!
Thanks
I haven't done it, and looking for a minute I don't see a plugin that does that for you. I think your best bet is to make a plugin that would change the theme variable based on the base URL. It's about as simple of a plugin as you can make, so it would be a good one to cut your teeth on if you have any programming ability whatsoever.
This might help:
http://codex.wordpress.org/Theme_Switching
There are 2 plugins noted in that url: http://wordpress.org/extend/plugins/theme-preview/
Theme preview which is what you want.
http://wordpress.org/extend/plugins/theme-switcher/
Theme switcher which do that too and create a sidebar widget so user can change the template.
I've found this good tutorial about base theme swithing on URL.
First, you have to catch the parameter in the URL:
function sjc_add_query_vars($vars)
{
return array('template') + $vars;
}
add_filter('query_vars', 'sjc_add_query_vars');
And then you serve the template:
function sjc_template($template)
{
global $wp;
if ($wp->query_vars['template']=='basic')
{
return dirname( __FILE__ ) . '/single-basic.php';
}
else
{
return $template;
}
}
add_filter('single_template', 'sjc_template');
Please note that the code above check if $wp->query_vars['template']=='basic' and not simply return the template specified in the URL, because it could be a security issue.
for who is still interested you have a WordPress functions that switches the theme
switch_theme( $stylesheet )
see https://codex.wordpress.org/Function_Reference/switch_theme
Just be care to call it in the right time, I would write it in a mu-plugin for use it.

Shortcode is not working in wordpress 3.0

Is there anything need to enable for shortcode to work in Wordpress 3.0? I have tried some short codes and none of them working as expected.
regards - dj
If its a template file you are trying to put them into you will need to put the code into a function and display it like:
<?php echo do_shortcode( '[contact-form-7 id="448" title="Contact"]' ); ?>
I am guessing you have it all sorted by now though as I have just checked when this was posted.
Short codes are as simple or as complex as you intend them to be. Also they can solve some pretty annoying problems and add a lot of functionality for your users. One thing I always use a short code for is embedding YouTube videos and media of the same format.
Here are some links that may help you:
Google Maps Short Code - by Chris Coyier # http://www.digwp.com/
Short Code API - WordPress Codex page about
short codes
Plus make sure you call the following function
the_content()
to output your content

Resources