What is this not saving the term - php

I am trying to add the term to favorites via a short code. However when I add the short code [add_term term_id=840"] (for example..where term id=840 is a one particular term). It's not adding to my list of favorited terms? I don't know why...
function add_termid( $atts, $content = null) {
$atts = shortcode_atts(
array(
'term_id' => 'term_id',
), $atts
);
}
function store()
{
global $userdata;
$favorites_to_store = array();
foreach($this->favorites as $favorite)
{
$favorites_to_store[] = array('term_id' => $favorite['term_id'], 'taxonomy' => $favorite['taxonomy']);
}
if(version_compare(get_bloginfo('version'), '3.0', '<')) update_usermeta($userdata->ID, 'wp_favorites', $favorites_to_store);
else update_user_meta($userdata->ID, 'wp_favorites', $favorites_to_store);
}
add_shortcode ('add_term', 'add_termid');

Related

WP add_filter within class constructor does not work

I'm using a basic PHP class to add callbacks to existing WC filters, outside the class the callbacks work as they should, but it seems the filters in the constructor do not call the functions, I tried many variations, but none of them work. This is the class:
class Filters implements FiltersInterface{
public function __construct()
{
add_filter("woocommerce_rest_product_object_query", array($this,'FilterByMeta'), 10,2);
add_filter("woocommerce_rest_query_var-tax_query", array($this,'FilterByTaxonomies'), 10,1);
}
public function FilterByMeta($args,$request){
$key = $request->get_param('meta-key');
if($key!='' && $key!=null)
{
$args['meta_key'] = $key;
}
$value = $request->get_param('meta-value');
if($value!='' && $value!=null)
{
$args['meta_value'] = $value;
}
return $args;
}
public function FilterByTaxonomies($args){
var_dump('--');
$attr_term = $_GET['attribute-term'];
$attr_name = $_GET['attribute-name'];
if($attr_term!='' && $attr_term!=null && $attr_name!='' && $attr_name!=null)
{
$args = array(
array(
'taxonomy' => $attr_name,
'field' => 'slug',
'terms' => $attr_term
)
);
return $args;
}
$cat= $_GET['category'];
if($cat!='' && $cat!=null)
{
$args = array(
'relation' => 'OR',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $cat
)
);
return $args;
}
return $args;
}
}
And this is in the index.php file i have in my custom 2-file plugin
include('cfilters.php');
$filters = new Filters;
I tried using the class name and 'CLASS 'in the add_filter, but it didnt work either
The problem was with the first filter not adding the key for the second one, after initializng the tax query in the first filter, it was working

php wordpress processing inside the hook and synchronizing the value to the variable outside the hook

what I want is that I have a special taxonomy and get_terms does not work without it being loaded, naturally the only way I can get it is to hook up to "init". But when this is the case, I will have to repeat this. I do not want this.
As you can see in the code below, I am doing the operation in init and trying to transfer it to "$ new_array". How can I do it?
protected function get_reactions()
{
$new_array = array();
add_action( 'init', function() use ( &$new_array ) {
$reactions = get_terms( array(
'taxonomy' => 'bp_reaction',
'hide_empty' => false
));
foreach ( $reactions as $value ) {
$priority = get_option( 'taxonomy_'.$value->term_id.'_priority' );
$image = get_option( 'taxonomy_'.$value->term_id.'_image' );
$new_array[$priority] = (object) array(
'id' => $value->term_id,
'priority' => $priority,
'slug' => $value->slug,
'name' => $value->name,
'image' => $image
);
}
}, 9 );
// Sort from largest to small
krsort( $new_array );
return $new_array;
}

OOP PHP method doesn't work when invoked within another method

I'm creating a PHP class for a Wordpress site. The following method works fine, but when I try to abstract it a bit by passing an argument and calling it within another method (see code further down below), it doesn't work anymore.
public function user_has_submitted_in_dog_category() {
$args = array(
'post_type' => 'submissions',
'author' => get_current_user_id(),
'tax_query' => array(
array(
'taxonomy' => 'submissions_categories',
'field' => 'slug',
'terms' => 'dog'
)
)
);
$user_posts = get_posts( $args );
if( count( $user_posts ) )
return true;
}
This is not working:
public function user_has_submitted_in_dog_category() {
$this->user_has_submitted_in_animal_category( 'dog' );
}
public function user_has_submitted_in_animal_category( $category ) {
$args = array(
'post_type' => 'submissions',
'author' => get_current_user_id(),
'tax_query' => array(
array(
'taxonomy' => 'submissions_categories',
'field' => 'slug',
'terms' => $category
)
)
);
$user_posts = get_posts( $args );
if( count( $user_posts ) )
return true;
}
By not working, I mean that user_has_submitted_in_dog_category() is not returning true. I call it in a template file as follows:
<?php if( $submission->user_has_submitted_in_dog_category() ) : ?>
<div class="msg">You have already submitted.</div>
<?php else : ?>
<div class="msg">You have not submitted yet.</div>
<?php endif; ?>
The block of code prints You have not submitted yet, but I do have posts in my custom taxonomy dog.
You need to return from your first (dog) method.
public function user_has_submitted_in_dog_category() {
return $this->user_has_submitted_in_animal_category( 'dog' );
}
Wordpress might be calling the method statically. You might be best off doing that yourself as the method doesn't seem to need an object instance to work:
public function user_has_submitted_in_dog_category() {
MyClass::user_has_submitted_in_animal_category( 'dog' );
}

in_array function not working when using variable?

In my below code I am trying to check whether the $query['skill'] value is contained in $c_skills. However it keeps failing. When I use the actual string that I am looking for, e.g. "test" then it functions as intended. My only thought is perhaps $query['skill'] doesn't return as a string, but I can't do a var_dump to find out since I am using wordpress and I am changing code to effect ajax query.
global $user_ID;
$query = $_REQUEST['query'];
if (isset($_REQUEST['query']['skill'])) {
if (isset($query['skill']) && $query['skill'] != '') {
global $wp_query, $ae_post_factory, $post, $current_user;
$post_object = $ae_post_factory->get(PROFILE);
$profile_id = get_user_meta( $user_ID, 'user_profile_id', true);
$profile = array('id' => 0, 'ID' => 0);
if($profile_id) {
$profile_post = get_post( $profile_id );
if($profile_post && !is_wp_error( $profile_post )){
$profile = $post_object->convert($profile_post);
}
}
$current_skills = get_the_terms( $profile, 'skill' );
if (!empty($current_skills)) {
$c_skills = array();
foreach ($current_skills as $key => $value) {
$c_skills[] = $value->name;
};
if(in_array( $query['skill'], $c_skills )) {
$query_args['tax_query'] = array(
'skill' => array(
'taxonomy' => 'skill',
'terms' => 'test',
'field' => 'slug'
)
);
}
}
I think your problem come from $query_args['tax_query'].
For me, the Key (skill) has nothing to do here.
Try to make it simple with
$query_args['tax_query'] = array(
array(
'taxonomy' => 'skill',
'terms' => 'test',
'field' => 'slug'
)
);
The place you use for skill between the two array is intended to be use for relation if you want to query more than one taxonomy.
Hope it works after this.

WordPress: Use shortcode_atts in other function

Is it possible to use variables from shortcode_atts in another function? Here is my idea:
Posting
[gallery ids="1,2,3,...n"]
Function get_gallery_ids()
//get the gallery-ID's from post
function get_gallery_ids($atts) {
extract(shortcode_atts(array(
'ids' => ''
), $atts));
return $ids;
}
Function explode_ids()
//example function with ids
function explode_ids($ids) {
$ids = explode(',' $ids);
}
How do I implement it? The return just echos.
Update
The code above is a part of my own new gallery_shortcode.
remove_shortcode('gallery', 'gallery_shortcode');
add_shortcode('gallery', 'get_gallery_ids');
I have an Idea:
I use examples from the WordPress Codex
http://codex.wordpress.org/Shortcode_API
function bartag_func( $atts ) {
global $post; // this is new
extract( shortcode_atts( array(
'foo' => 'something',
'bar' => 'something else',
), $atts ) );
update_post_meta($post->ID, "gallery_used_atts", $atts); // this is new
return "foo = {$foo}";
}
add_shortcode( 'bartag', 'bartag_func' );
now you are able to get the $atts you are using on a specific post or page by
$att_values = get_post_meta( $post_id, "gallery_used_atts", true );
and then you can use them for whatever you want.

Categories