Hello All,
Looking for some help with my aggregator website. I wanted to know if its possible to have post has a certain tag get automatically assigned to a certain user.
Example: Post has tag "ABC" -> When published it will have user "ABC" as the author.
is this possible? maybe with a condition?
I would need it to work for multiple users
Kyle answers:
add_action('publish_post','assign_author_by_tag');
function assign_author_by_tag($post_id){
$post = get_post($post_id);
$tag_id = ''; //insert tag id here
$author_id = ''; //insert the authors user id here
if( has_tag( $tag_id, $post ) ){
$my_post = array(
'ID' => $post_id,
'post_author' => $author_id
);
wp_update_post( $my_post );
}
}
Kyle comments:
You would probably have to have a few different checks, one for each tag and correlating author.
Kyle comments:
If you are using a custom taxonomy, like product tags, you can use has_term instead of has_tag like Arnav has below
Kyle comments:
*has_term
George Sprouse comments:
yea i i think i would need to do a different checks as i have many tags and many users. Do you know how i could do that?
Kyle comments:
Basically, just repeat the middle part there for each:
add_action('publish_post','assign_author_by_tag');
function assign_author_by_tag($post_id){
$post = get_post($post_id);
$tag_id_1 = ''; //insert tag id here
$author_id_1 = ''; //insert the authors user id here
if( has_tag( $tag_id_1, $post ) ){
$my_post_1= array(
'ID' => $post_id,
'post_author' => $author_id_1
);
wp_update_post( $my_post_1 );
}
$tag_id_2 = ''; //insert tag id here
$author_id_2 = ''; //insert the authors user id here
if( has_tag( $tag_id_2, $post ) ){
$my_post_2 = array(
'ID' => $post_id,
'post_author' => $author_id_2
);
wp_update_post( $my_post_2 );
}
$tag_id_3 = ''; //insert tag id here
$author_id_3 = ''; //insert the authors user id here
if( has_tag( $tag_id_3, $post ) ){
$my_post_3 = array(
'ID' => $post_id,
'post_author' => $author_id_3
);
wp_update_post( $my_post_3 );
}
}
George Sprouse comments:
Hey Kyle,
Thanks that works but the server crashes whenever i add a post with the tag I add in the code. Though if i look in my post list it was published. If i add a post without any of the tags mention it does not crash
any way to get around this?
Kyle comments:
Can you post your code that is causing it to crash and I'll have a look
George Sprouse comments:
Thanks Kyle
Here is my full functions file
<?php
/**
* Theme Functions
*
* @package Shaken Grid (Premium)
* @since 1.0
* @alter 2.1.3
*
*/
add_action( 'after_setup_theme', 'shaken_setup' );
/**
* Sets up theme defaults and registers support for various WordPress features.
*
* Note that this function is hooked into the after_setup_theme hook, which runs
* before the init hook. The init hook is too late for some features, such as indicating
* support post thumbnails.
*
* To override shaken_setup() in a child theme, add your own shaken_setup to your child theme's
* functions.php file.
*
* @since 1.5.0
*/
if(!function_exists('shaken_setup')):
function shaken_setup() {
// Theme support
if ( ! isset( $content_width ) ){
$content_width = 620;
}
// Enable support for default WordPress components
add_theme_support( 'post-formats', array( 'quote', 'gallery' ) );
add_editor_style();
add_theme_support('automatic-feed-links');
add_custom_background('shaken_custom_background_cb');
// Set featured image sizes
add_theme_support( 'post-thumbnails');
set_post_thumbnail_size( 320, 1800);
/**
* Support added for theme specific components
* To remove support, create a child theme and use remove_theme_support()
* in its functions.php file.
*
* We can remove the parent theme's hook only after it is attached, which means we need to
* wait until setting up the child theme:
* add_action( 'after_setup_theme', 'my_child_theme_setup' );
* function my_child_theme_setup() {
* // We are removing support for the hover buttons
* remove_theme_support('shaken_action_buttons');
* ...
* }
* @since 1.5.0
*/
// Buttons shown on image hover
add_theme_support('shaken_action_buttons');
// Footer credit text
add_theme_support('shaken_footer_credit');
/* Uncomment the line below to enable comments
on all page templates. */
//add_theme_support('shaken_page_comments');
// Actions
/* Add your nav menus function to the 'init' action hook. */
add_action( 'init', 'shaken_register_menus' );
/* Add your sidebars function to the 'widgets_init' action hook. */
add_action( 'widgets_init', 'shaken_register_sidebars' );
// Threaded comments
add_action('get_header', 'enable_threaded_comments');
// Customize dashboard widgets
add_action('wp_dashboard_setup', 'shaken_dashboard_widgets');
// Filters
// No more jumping on Read More link
add_filter('excerpt_more', 'no_more_jumping');
// Show home link in wp_nav_menu() fallback
add_filter( 'wp_page_menu_args', 'shaken_page_menu_args' );
// Add featured images to RSS
add_filter('pre_get_posts','feedFilter');
// Add wmode='transparent' to auto embedded Flash videos
add_filter('embed_oembed_html', 'add_video_wmode', 10, 3);
// Allow shortcodes in text widgets
add_filter('widget_text', 'shortcode_unautop');
add_filter('widget_text', 'do_shortcode');
/* Make theme available for translation
* Translations can be filed in the /languages/ directory */
load_theme_textdomain( 'shaken', TEMPLATEPATH . '/languages' );
$locale = get_locale();
$locale_file = TEMPLATEPATH . "/languages/$locale.php";
if ( is_readable( $locale_file ) )
require_once( $locale_file );
}
endif;
/**
* shaken_custom_background_cb()
* Create a callback for custom backgrounds
* Removes the old background so the user defined background can display.
*
* @since 1.6.0
**/
if(!function_exists('shaken_custom_background_cb')){
function shaken_custom_background_cb() {
$background = get_background_image();
$color = get_background_color();
if ( ! $background && ! $color )
return;
$style = $color ? "background-color: #$color;" : '';
if ( $background ) {
$image = " background-image: url('$background');";
$repeat = get_theme_mod( 'background_repeat', 'repeat' );
if ( ! in_array( $repeat, array( 'no-repeat', 'repeat-x', 'repeat-y', 'repeat' ) ) )
$repeat = 'repeat';
$repeat = " background-repeat: $repeat;";
$position = get_theme_mod( 'background_position_x', 'left' );
if ( ! in_array( $position, array( 'center', 'right', 'left' ) ) )
$position = 'left';
$position = " background-position: top $position;";
$attachment = get_theme_mod( 'background_attachment', 'scroll' );
if ( ! in_array( $attachment, array( 'fixed', 'scroll' ) ) )
$attachment = 'scroll';
$attachment = " background-attachment: $attachment;";
$style .= $image . $repeat . $position . $attachment;
}
?>
<style type="text/css">
body {background:none; <?php echo trim( $style ); ?> }
</style>
<?php }
}
/**
* Get our wp_nav_menu() fallback, wp_page_menu(), to show a home link.
*
* To override this in a child theme, remove the filter and optionally add
* your own function tied to the wp_page_menu_args filter hook.
*
*/
function shaken_page_menu_args( $args ) {
$args['show_home'] = true;
return $args;
}
function wptuts_load_caroufredsel() {
// Enqueue carouFredSel, note that we specify 'jquery' as a dependency, and we set 'true' for loading in the footer:
wp_register_script( 'caroufredsel', get_template_directory_uri() . '/js/jquery.carouFredSel-6.1.0-packed.js', array( 'jquery' ), '6.1.0', true );
// For either a plugin or a theme, you can then enqueue the script:
wp_enqueue_script( 'wptuts-caroufredsel', get_template_directory_uri() . '/js/wptuts-caroufredsel.js', array( 'caroufredsel' ), '', true );
}
add_action( 'wp_enqueue_scripts', 'wptuts_load_caroufredsel' );
function jquery_load_sharrre() {
// Register Sharrre
wp_register_script( 'sharrre', get_template_directory_uri() . '/js/jquery.sharrre-1.3.4.min.js', array(), '', true );
wp_register_script( 'sharre', get_template_directory_uri() . '/js/jquery.sharrre2-1.34.min.js', array(), '', true );
wp_register_script( 'backstretch', get_template_directory_uri() . '/js/jquery.backstretch.min.js', array(), '', true );
// Register sharrre initialiser script
wp_register_script( 'jquery-shareme', get_template_directory_uri() . '/js/jquery-shareme.js', array('jquery', 'sharrre'), '', true );
wp_register_script( 'jquery-shareme-2', get_template_directory_uri() . '/js/jquery-shareme-2.js', array('jquery', 'sharre','backstretch'), '', true );
// Now enqueue Sharrre
wp_enqueue_script( 'jquery-shareme' );
wp_enqueue_script( 'jquery-shareme-2' );
// Register sharrre CSS
wp_register_style( 'jquery-shareme', get_template_directory_uri() . '/css/jquery-shareme.css');
// Now enqueue sharrre
wp_enqueue_style( 'jquery-shareme' );
}
add_action( 'wp_enqueue_scripts', 'jquery_load_sharrre' );
function shaken_register_menus(){
register_nav_menus( array(
'header' => __( 'Header Menu'),
) );
}
// -------------- Register Menus --------------
function shaken_register_sidebars(){
register_sidebar( array (
'name' => 'Page Sidebar',
'id' => 'page-sidebar',
'description' => __( 'The sidebar on basic pages'),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => "</div>",
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
register_sidebar( array (
'name' => "Unique Sidebar",
'id' => 'unique-sidebar',
'description' => __( 'The sidebar on pages with the template of "Unique Sidebar" assigned to them.'),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => "</div>",
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
register_sidebar( array (
'name' => "Blog Post Sidebar",
'id' => 'post-sidebar',
'description' => __( 'The sidebar on blog post pages'),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => "</div>",
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
register_sidebar( array (
'name' => 'Blog Sidebar',
'id' => 'gallery-sidebar',
'description' => __( 'The sidebar on the gallery and archive pages'),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => "</div>",
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
register_sidebar( array (
'name' => 'Author Sidebar',
'id' => 'author-sidebar',
'description' => __( 'The sidebar on the author pages'),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => "</div>",
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
}
// smart jquery inclusion
function shaken_jquery(){
if (!is_admin()) {
wp_enqueue_script('jquery');
}
}
add_action( 'wp_enqueue_scripts', 'shaken_jquery' );
// enable threaded comments
function enable_threaded_comments(){
if (!is_admin()) {
if (is_singular() AND comments_open() AND (get_option('thread_comments') == 1))
wp_enqueue_script('comment-reply');
}
}
// no more jumping for read more link
if(!function_exists('no_more_jumping')){
function no_more_jumping($post) {
return ' <a href="'.get_permalink($post->ID).'" class="read-more">'.'->'.'</a>';
}}
// -------------- Add featured images to RSS feed --------------
if(!function_exists('feedContentFilter')){
function feedContentFilter($content) {
$thumbId = get_post_thumbnail_id();
$embed_code = get_post_meta(get_the_id(), 'soy_vid', true);
$vid_url = get_post_meta(get_the_id(), 'soy_vid_url', true);
if($thumbId) {
$img = wp_get_attachment_image_src($thumbId, 'col3');
$image = '<img src="'. $img[0] .'" alt="" width="'. $img[1] .'" height="'. $img[2] .'" />';
echo $image;
}
if($embed_code){
echo $embed_code;
} else if($vid_url){
echo '<p><strong><a href="'.$vid_url.'">View Video</a></strong></p>';
}
return $content;
}}
if(!function_exists('feedFilter')){
function feedFilter($query) {
if ($query->is_feed) {
add_filter('the_content', 'feedContentFilter');
}
return $query;
}}
// Add S&S RSS feed to dashboard
function shaken_rss_output(){
echo '<div class="rss-widget">';
wp_widget_rss_output(array(
'url' => 'http://feeds.feedburner.com/shakenandstirredweb/MLnE', //put your feed URL here
'title' => 'Latest News from Shaken & Stirred', // Your feed title
'items' => 5, //how many posts to show
'show_summary' => 1, // 0 = false and 1 = true
'show_author' => 0,
'show_date' => 0
));
echo "</div>";
}
// Add text dashboard widget
function shaken_twitter_dash_output(){
echo '<div class="text-widget">';
echo '<p>Follow Shaken and Stirred on <strong><a href="http://twitter.com/shakenweb" target="_blank">Twitter (@shakenweb)</a></strong> to stay up to date with the latest theme updates and new releases. You can also <strong><a href="http://shakenandstirredweb.com" target="_blank">visit our website</a></strong> to read our Tips & Tricks to get the most out of your theme. We hope you enjoy our theme!</p>';
echo "</div>";
}
// Add and remove dashboard widgets
function shaken_dashboard_widgets(){
// Add custom widgets
wp_add_dashboard_widget( 'shaken-twitter', 'Stay Updated!', 'shaken_twitter_dash_output');
wp_add_dashboard_widget( 'shaken-rss', 'Latest News from Shaken & Stirred', 'shaken_rss_output');
}
function get_user_role() {
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
return $user_role;
}
function add_twitterUser($contactmethods) {
// Add Twitter Username
$contactmethods['twitter'] = 'Twitter Username';
return $contactmethods;
}
add_filter('user_contactmethods','add_twitterUser',10,1);
add_filter( 'user_contactmethods', 'update_contact_methods',10,1);
function update_contact_methods( $contactmethods ) {
unset($contactmethods['aim']);
unset($contactmethods['jabber']);
unset($contactmethods['yim']);
return $contactmethods;
}
function function_new_user($user_id) {
add_user_meta( $user_id, '_new_user', '1' );
}
add_action( 'user_register', 'function_new_user');
function function_check_login_redirect($user_login, $user) {
$logincontrol = get_user_meta($user->ID, '_new_user', 'TRUE');
if ( $logincontrol ) {
//set the user to old
update_user_meta( $user->ID, '_new_user', '0' );
//Do the redirects or whatever you need to do for the first login
wp_redirect( 'http://www.watchthefeed.com/profile', 302 ); exit;
}
}
add_action('wp_login', 'function_check_login_redirect', 10, 2);
// show admin bar only for admins
if (!current_user_can('manage_options')) {
add_filter('show_admin_bar', '__return_false');
}
/**Exclude certain categories from home page loop */
function exclude_categories($query) {
if ( $query->is_home ) {
$query->set('cat', '-30,-27,-6');
}
return $query;
}
add_filter('pre_get_posts', 'exclude_categories');
function get_author_role()
{
global $authordata;
$author_roles = $authordata->roles;
$author_role = array_shift($author_roles);
return $author_role;
}
function remove_comment_fields($fields) {
unset($fields['url']);
return $fields;
}
add_filter('comment_form_default_fields','remove_comment_fields');
remove_filter('comment_text', 'make_clickable', 9);
// add custom feed content
function add_feed_content($content) {
if(is_feed()) {
$content .= '<p>This article is copyright of http://www.watchthefeed.com © '.date('Y').' '.bloginfo('name').'</p>';
}
return $content;
}
add_filter('the_excerpt_rss', 'add_feed_content');
add_filter('the_content', 'add_feed_content');
function contributors() {
global $wpdb;
$authors = $wpdb->get_results("SELECT ID, user_nicename from $wpdb->users WHERE display_name <> 'admin' ORDER BY display_name");
foreach ($authors as $author ) {
echo "<li>";
echo "<a href=\"".get_bloginfo('url')."/author/";
the_author_meta('user_nicename', $author->ID);
echo "/\">";
echo userphoto($author->ID);
echo "</a>";
echo '<div>';
echo "<a href=\"".get_bloginfo('url')."/author/";
the_author_meta('user_nicename', $author->ID);
echo "/\">";
the_author_meta('display_name', $author->ID);
echo "</a>";
echo "<br />";
echo "Website: <a href=\"";
the_author_meta('user_url', $author->ID);
echo "/\" target='_blank'>";
the_author_meta('user_url', $author->ID);
echo "</a>";
echo "<br />";
echo "Twitter: <a href=\"http://twitter.com/";
the_author_meta('twitter', $author->ID);
echo "\" target='_blank'>";
the_author_meta('twitter', $author->ID);
echo "</a>";
echo "<br />";
echo "<a href=\"".get_bloginfo('url')."/author/";
the_author_meta('user_nicename', $author->ID);
echo "/\">View ";
the_author_meta('display_name', $author->ID);
echo "'s Posts";
echo "</a>";
echo "</div>";
echo "</li>";
}
}
function custom_theme_js(){
wp_register_script( 'infinite_scroll', get_template_directory_uri() . '/js/jquery.infinitescroll.min.js', array('jquery'),null, true );
if( ! is_singular() ) {
wp_enqueue_script('infinite_scroll');
}
}
add_action('wp_enqueue_scripts', 'custom_theme_js');
function _remove_script_version( $src ){
$parts = explode( '?ver', $src );
return $parts[0];
}
add_filter( 'script_loader_src', '_remove_script_version', 15, 1 );
add_filter( 'style_loader_src', '_remove_script_version', 15, 1 );
// Modify the $tag_categories array. Use one tag name => one category name
function mam_add_category ($post_id = 0) {
if (!$post_id) return;
$tag_categories = array ( // The tag names and the corresponding category name to add
'outfeed' => 'uncategorized',
);
$post_tags = get_the_tags($post_id);
foreach ($post_tags as $tag) {
if ($tag_categories[$tag->name] ) {
$cat_id = get_cat_ID($tag_categories[$tag->name]);
if ($cat_id) {
$result = wp_set_post_terms( $post_id, $tags = $cat_id, $taxonomy = 'category', $append = true );
}
}
}
}
add_action('publish_post','mam_add_category');
add_filter( 'embed_defaults', 'bigger_embed_size' );
function bigger_embed_size()
{
return array( 'width' => 610, 'height' => 345 );
}
function ajx_sharpen_resized_files( $resized_file ) {
$image = wp_load_image( $resized_file );
if ( !is_resource( $image ) )
return new WP_Error( 'error_loading_image', $image, $file );
$size = @getimagesize( $resized_file );
if ( !$size )
return new WP_Error('invalid_image', __('Could not read image size'), $file);
list($orig_w, $orig_h, $orig_type) = $size;
switch ( $orig_type ) {
case IMAGETYPE_JPEG:
$matrix = array(
array(-1, -1, -1),
array(-1, 16, -1),
array(-1, -1, -1),
);
$divisor = array_sum(array_map('array_sum', $matrix));
$offset = 0;
imageconvolution($image, $matrix, $divisor, $offset);
imagejpeg($image, $resized_file,apply_filters( 'jpeg_quality', 90, 'edit_image' ));
break;
case IMAGETYPE_PNG:
return $resized_file;
case IMAGETYPE_GIF:
return $resized_file;
}
return $resized_file;
}
add_filter('image_make_intermediate_size', 'ajx_sharpen_resized_files',900);
function delete_post_media( $post_id ) {
$attachments = get_posts( array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_status' => 'any',
'post_parent' => $post_id
) );
foreach ( $attachments as $attachment ) {
if ( false === wp_delete_attachment( $attachment->ID ) ) {
// Log failure to delete attachment.
}
}
}
add_action('before_delete_post', 'delete_post_media');
function enqueue_magnific_popup_styles() {
wp_register_style( 'magnific_popup_style', get_stylesheet_directory_uri() . '/magnific-popup/magnific-popup.css' );
wp_enqueue_style( 'magnific_popup_style' );
}
add_action('wp_enqueue_scripts', 'enqueue_magnific_popup_scripts');
function enqueue_magnific_popup_scripts() {
wp_register_script( 'magnific_popup_script', get_stylesheet_directory_uri() . '/magnific-popup/jquery.magnific-popup.js', array( 'jquery' ) );
wp_enqueue_script( 'magnific_popup_script' );
wp_register_script( 'magnific_init_script', get_stylesheet_directory_uri() . '/magnific-popup/jquery.magnific-init.js', array( 'jquery' ) );
wp_enqueue_script( 'magnific_init_script' );
}
//ASSIGN AUTHOR WITH TAG
add_action('publish_post','assign_author_by_tag');
function assign_author_by_tag($post_id){
$post = get_post($post_id);
$tag_id_1 = '8160'; //insert tag id here
$author_id_1 = '40'; //insert the authors user id here
if( has_tag( $tag_id_1, $post ) ){
$my_post_1= array(
'ID' => $post_id,
'post_author' => $author_id_1
);
wp_update_post( $my_post_1 );
}
$tag_id_2 = ''; //insert tag id here
$author_id_2 = ''; //insert the authors user id here
if( has_tag( $tag_id_2, $post ) ){
$my_post_2 = array(
'ID' => $post_id,
'post_author' => $author_id_2
);
wp_update_post( $my_post_2 );
}
$tag_id_3 = ''; //insert tag id here
$author_id_3 = ''; //insert the authors user id here
if( has_tag( $tag_id_3, $post ) ){
$my_post_3 = array(
'ID' => $post_id,
'post_author' => $author_id_3
);
wp_update_post( $my_post_3 );
}
}
// -------------- Theme Options Panel --------------
require_once(TEMPLATEPATH . '/functions/framework-init.php');
?>
Kyle comments:
Ya know, that's my fault. I had an issue like this before. The problem is that this causes a loop, so you have to remove the action before update post each time. Each one will have to look like this:
add_action('save_post','assign_author_by_tag');
function assign_author_by_tag($post_id){
$post = get_post($post_id);
$tag_id_1 = '8160'; //insert tag id here
$author_id_1 = '40'; //insert the authors user id here
if( has_tag( $tag_id_1, $post ) ){
$my_post_1= array(
'ID' => $post_id,
'post_author' => $author_id_1
);
remove_action('save_post', 'assign_author_by_tag');
wp_update_post( $my_post_1 );
add_action('save_post', 'assign_author_by_tag');
}
}
George Sprouse comments:
Great that worked awesome! Thanks for taking the extra look appreciate it
Arnav Joy answers:
try this
<?php
function my_assgin_author_post( $post_id ) {
$term_array_to_check = array('ABC');
if( has_term( $term_array_to_check , 'post_tag' , $post_id ) ) {
$my_post = array(
'ID' => $post_id,
'post_author' => 111 // author id
);
wp_update_post( $my_post );
}
}
add_action( 'save_post', 'my_assgin_author_post' );
here is the function reference
http://codex.wordpress.org/Function_Reference/has_term
George Sprouse comments:
Hi Arnav,
How can i check for multiple tags and authors? I think i would need to do a different checks as i have many tags and many users. Do you know how i could do that?
Arnav Joy comments:
try this
<?php
function my_assgin_author_post( $post_id ) {
$post_data = get_post( $post_id );
if( has_term( 'ABC' , 'post_tag' , $post_id ) )
$author_id = 1;
else if( has_term( 'XYZ' , 'post_tag' , $post_id ) )
$author_id = 2;
else if( has_term( 'MNO' , 'post_tag' , $post_id ) )
$author_id = 3;
else
$author_id =$post_data->post_author;
$my_post = array(
'ID' => $post_id,
'post_author' => $author_id
);
wp_update_post( $my_post );
}
add_action( 'save_post', 'my_assgin_author_post' );
George Sprouse comments:
Thanks that works but the server crashes whenever i add a post with the tag I add in the code. Though if i look in my post list it was published. If i add a post without any of the tags mention it does not crash
any way to get around this?
Thomas Varghese answers:
Tags are usually assigned with post and not with user. The one possible solution for this is that you can store a tag associated to specifiec user in a user meta. And then you can reterive from the user meta and check conditionally and do what you want to do.
Just Me answers:
Not sure if it is going to change a lot, but if it is, it is not that functional when you have to edit your functions.php file every time.
It would be best to create a taxonomy/custom type with 'add/edit/delete' functions in the admin section.
In your code you would only have a function checking the entries and adjust the author accordingly.
Just my 2cts