Automatically add a group of tags to NEW wordpress posts - wordpress

I have a blog where I make alot of posts every day. Many of the posts use 5 of 10 commonly used tags. Instead of writing out each of these tags every time, I'd rather just uncheck a few of the tags I don't need (and then add any new, or uncommon tags that I have).
So, I'm looking to edit my functions.php document so that any NEW posts will already have a list of 10 tags in it. And, if tags already exists, don't do anything. I'd like to avoid using a plugin, if possible.
Anyone know how to do this? This would be extremely useful.
The code would look something like:
(note: my programming skills are horrendous, so this may not be right, or even possible)
function default_tag_list() {
if(new_post() && !get_the_tag_list()) { // using new_post() ... not sure how to check if its a new post.
$default_tags = array('health', 'nutrition', 'diet', 'well-being', 'eating');
return $default_tags;
} // Then, somehow get the get_tag_list in the administration to use the default tags function (if it's a new post without any tags) ...
}
}

Just copy and paste the below code in your function.php :
function my_data_update () {
GLOBAL $post;
wp_set_post_tags( $post->ID, 'health, nutrition, diet, well-being, eating', true );
}
add_action('publish_post', 'my_data_update');
add_action('save_post', 'my_data_update');
this code will definitely work for you as it is tested.

Related

Disabling HTML on Comments

I've been researching for the past few hours, trying to find a way to disable HTML in WordPress comments. So far this one consistently appeared on top of Google search results numerous times:
// This will occur when the comment is posted
function plc_comment_post( $incoming_comment ) {
// convert everything in a comment to display literally
$incoming_comment['comment_content'] = htmlspecialchars($incoming_comment['comment_content']);
// the one exception is single quotes, which cannot be #039; because WordPress marks it as spam
$incoming_comment['comment_content'] = str_replace( "'", ''', $incoming_comment['comment_content'] );
return( $incoming_comment );
}
// This will occur before a comment is displayed
function plc_comment_display( $comment_to_display ) {
// Put the single quotes back in
$comment_to_display = str_replace( ''', "'", $comment_to_display );
return $comment_to_display;
This code did not work with the latest version of WordPress. I also found many more codes that again, did not work. So how would one go about disabling HTML in WordPress 3.6 (the latest version) comments?
This removed the ability for users to post HTML (but not links for some strange reason) within comments:
add_filter( 'pre_comment_content', 'wp_specialchars' );
This removed the ability for users to post links within comments:
remove_filter('comment_text', 'make_clickable', 9);
To disable HTML tags in comments, put the following code into your theme's functions.php:
add_filter('comment_text', 'wp_filter_nohtml_kses');
add_filter('comment_text_rss', 'wp_filter_nohtml_kses');
add_filter('comment_excerpt', 'wp_filter_nohtml_kses');

Wordpress is single undefined

I'm creating a new wordpress plugin, which only be displayed in posts, but to detect it's a post, I'm trying to use is_single(), but it does not work.
class myplugin{
//my plugin code here
}
function load_plugin($plugin_class, $priority = 10) {
if (class_exists($plugin_class)) {
add_action("init",
create_function('', "global \$$plugin_class; \$$plugin_class = new $plugin_class();"),
$priority);
}
}
if(is_single()){ // witout this, the plugin is displayed everywhere, but whit it it's not displayed at all
load_plugin(myplugin);
}
I even tried to see the output of is_single
echo"<script>alert('".is_single()."');</script>";
i get "undefined"
edit // witout the is_single and just loading my plugin, my plugin works on every page of wordpress.
Conditional tags, like is_single, are not available until the the wp hook has fired. You're trying to use it too early, which is why it returns undefined.
Add your function to a hook after that and do the is_single test there. There is very little overhead in this so don't worry about performance issues.

Remove screen options tab if not admin

As the title suggests, I am looking for a way to remove the screen options tab in the post/page editor screen. I have found the following...
function remove_screen_options(){ __return_false;}
add_filter('screen_options_show_screen', 'remove_screen_options');
...but it removes the tab for all users. I would like to keep it for admins.
Regards,
John
Found the answer after collaboration of all of your efforts. Thank you.
get_currentuserinfo() ;
global $user_level;
function remove_screen_options(){ __return_false;}
if( $user_level <= 8 ) add_filter('screen_options_show_screen', 'remove_screen_options');
If you place the following snippet of code into your functions.php file, the Screen Options tab will disappear across the whole backend for all users except the admin. Having said that, it is a good practice to modify the php file of your child theme.
functions.php code:
function remove_screen_options_tab()
{
return current_user_can('manage_options' );
}
add_filter('screen_options_show_screen', 'remove_screen_options_tab');
You just need to conditionally check if the current user is an admin. If they aren't, then remove the screen options like so:
if ( !is_admin() ) {
function remove_screen_options(){ __return_false;}
add_filter('screen_options_show_screen', 'remove_screen_options');
}
Here are the official Wordpress docs detailing this function: http://codex.wordpress.org/Function_Reference/is_admin
Using capabilities the way Spencer suggests is usually the best method.
I'm thinking most people find roles & capabilities to be overly confusing. Using the actual user role with 'current_user_can' is more often than not your best bet for this or any other similar 'permission' based situation.
More often than not you will eventually end up adding/removing capabilities for a specific role, so if you ever give someone else the 'manage_options' capability, like perhaps the owner of the business, you all of a sudden gave them back the screen options as well (On a side note 'activate_plugins' is usually safe since it is only for someone that has level 10 access). You can check out all permissons on the User Level Capability Table at the bottom of the page to get a better grasp on it all.
Insert this in functions php:
if( !current_user_can('administrator') ) {
// hide screen options for everyone but the admin
add_filter('screen_options_show_screen', 'remove_screen_options_tab');
}
if( current_user_can('administrator') ) {
// code here is shown to the admin
}
Following this format you can do the same thing with other roles. Also you dont have is change administrator to editor, author, contributor and subscriber, or any other roles you create.
Try this
if(!current_user_can('manage_options')) add_filter('screen_options_show_screen', 'remove_screen_options');
Use Adminimize, A WordPress plugin that lets you hide 'unnecessary' items from the WordPress backend.
http://wordpress.org/plugins/adminimize/
You're looking for the function current_user_can:
if( current_user_can( 'manage_options' ) ) {
// executes when user is an Administrator
}
Here's the CSS method for all the designers who rather stay away from php. It hooks into the admin_body_class and adds user-{role} as a body class.
functions.php code:
function hide_using_css_user_role( $classes ) {
global $current_user;
foreach( $current_user->roles as $role )
$classes .= ' user-' . $role;
return trim( $classes );
}
add_filter( 'admin_body_class', 'hide_using_css_user_role' );
Using this you can hide/show anything on the admin side per user role. In this case just use the :not css selector to make sure it's only hidden for non-admins.
function add_my_custom_user_css() {
ob_start(); ?>
<style type="text/css">
:not(.user-administrator) #screen-options-link-wrap,
:not(.user-administrator) #contextual-help-link-wrap  {
display:none !important;
}
</style>
<?php
echo ob_get_clean();
}
add_action ( 'admin_head', 'add_my_custom_user_css', 999);
This is a pretty hacky way to do things but sometimes good for a temporary quick fix when you don't know the correct filter/action to hide or change things in wordpress. Adding the 999 will make sure it gets loaded at the end of the head tag. Note that it's only hiding using css, so don't use this for anything vitally important, because the code is still visible in the source file.
To remove it from the source use jquery instead. Just replace the styles above with the following:
<script type="text/javascript">
jQuery(document).ready(function($)) {
$( ":not(.user-administrator) #screen-options-link-wrap" ).remove();
}
</script>
'admin_body_class' already does us the favor of adding the page to body class, so to target specific pages as well, just check the source code and in the body tag you can see the current page. So for example, the dashboard uses .index-php. Just attach that to .user-administrator or whatever user you're targeting and you can customize the admin for any user just using css and javascript.

WordPress template_include - how to hook it properly

I'm currently coding a WP plugin and would need to override templates.
My filter hook looks like that - and it executes:
add_filter('template_include', 'mcd_set_template',10);
function mcd_set_template() just returns the required path as string - or the default WP template in case the file not exists.
I'm toying with this for hours already, even could include that alternate template (but it appears at the bottom of the page).
So my question is, how to force WP 3.2.1 to just load another template file instead - and which priority is required??
Update:
Also I noticed when using var_dump ... it outputs almost at the end of the file - but should appear before the opening HTML tag...
According to this ticket it should work with template_include hook: http://core.trac.wordpress.org/ticket/11242
Or is the only way to hook these filters instead:
http://codex.wordpress.org/Template_Hierarchy#Filter_Hierarchy
?
You could use template_redirect as shown above, but that does require exit, and it does trample on everything else WordPress would normally do to find the current template. You may want to let that happen and then apply logic to the current template.
Using some of what is above...
add_action('template_include', 'mcd_set_template');
function mcd_set_template() {
return locate_template('templatename.php');
}
That is fairly simple, you can also pass an array to locate_template() to define a hierarchy. If you were to use 'template_redirect as shown above, you should still be using locate_template, and this is how.
add_action('template_redirect', 'mcd_set_template');
function mcd_set_template() {
/**
* Order of templates in this array reflect their hierarchy.
* You'll want to have fallbacks like index.php in case yours is not found.
*/
$templates = array('templatename.php', 'othertemplate.php', 'index.php');
/**
* The first param will be prefixed to '_template' to create a filter
* The second will be passed to locate_template and loaded.
*/
include( get_query_template('mcd', $templates) );
exit;
}
Finally, the best way would be to filter specific types instead of the whole hierarchy. For example you could filter 'category_template' or 'page_template'. That would be more specific, it would avoid messing with the whole template hierarchy if you don't want to - and it lets WordPress do more of the heavy lifting
For example:
add_filter('category_template', 'filter_category_template');
function filter_category_template($template){
/* Get current category */
$category = get_queried_object();
/* Create hierarchical list of desired templates */
$templates = array (
'category.php',
'custom-category-template.php',
'category-{$category->slug}.php',
'category-{$category->term_id}.php',
'index.php'
);
return locate_template($templates);
}
You can of course create that array of hierarchical templates any time you use locate_template(). Using this method, its easy to see how easily you could create all sorts of very detailed and specific hierarchies either as part of or separate from the native Template Hierarchy.
Have you tried using an add_action instead? For example, you might want to try something like the following in your plugin:
add_action('template_redirect', 'mcd_set_template');
//Redirect to a preferred template.
function mcd_set_template() {
$template_path = TEMPLATEPATH . '/' . "templatename.php";
if(file_exists($template_path)){
include($template_path);
exit;
}
}
Here is a helpful reference: http://www.mihaivalentin.com/wordpress-tutorial-load-the-template-you-want-with-template_redirect/

Why isn't $vars['node'] available in preprocess_page for some content types?

I am currently using drupal 6 for a site I'm working on. I have a MYTHEME_preprocess_page() function that adds a few variables to the page.tpl.php template from the taxonomy and from a cck field. It was working correctly for a bit, and then the $vars['node'] is empty, but only for 2 content types. The 'node' variable is available to the preprocess_page function in other content types.
I thought it was a problem with using the following code, but when I remove all of this, the 'node' variable is still empty.
function mytheme_preprocess_node(&$vars, $hook) {
$function = 'mytheme_preprocess_node'.'_'. $vars['node']->type;
if (function_exists($function)) {
$function(&$vars);
}
}
Does anyone know of any gotchas or bugs that might be removing the 'node' variable? I can't seem to figure out where I'm going wrong. I'm at a loss.
Here is my complete mytheme_preprocess_page() function.
function mytheme_preprocess_page(&$vars, $hook) {
if ($hook == 'node' || $hook == 'page') {
if (is_object($vars['node'])) {
// grab the header image if it exists to make it avaialble to the content header
$vars['header_image'] = _mytheme_get_header_image($vars);
// get the taxonomy term to put in the content header
if (count($vars['node']->taxonomy) > 0) {
$vars['tax_term'] = "<div class=\"terms\">" . _mytheme_get_first_taxonomy_term($vars['node']->taxonomy) . "</div>";
}
// add the teacher's credentials to the content header
if ($vars['node']->field_credentials[0]['view'] != '') {
$vars['teacher_credentials'] = '<span class="teacher-creds">' . $vars['node']->field_credentials[0]['view'] . '</span>';
}
}
}
}
After going through and disabling modules one-by-one, I determined that the problem is related to the module, node_breadcrumb. A similar issue was filed here: http://drupal.org/node/616100#comment-2199374
In the 3rd comment, you'll see a link to another issue with a resolution
For others that run into this, I had the same issue as a result of using the jQuery UI module. Disabling and re-enabling fixed it, and I could not track down the specific issue, but it appeared to be related to $static variables in some path check functions.
To others that stumble their way into here, I suggest you pull some of the more obvious modules right out of the module folder on your dev setup, see if things change, and then put them back in there until you figure it out.
Another option is to search for instances of _preprocess_page(, $variables['node'] and $vars['node'] to see if some contributed code is unwittingly unsetting a node when it shouldn't be.

Resources