Skip Navigation

[Resolved] Displaying content based on parameters

This support ticket is created 7 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 -
- - - - - - -

Supporter timezone: Asia/Karachi (GMT+05:00)

Author
Posts
#336494

Hi
Im building up a control panel with 2 buttons.
Once button A is selected all the CPT on the web site will display the content inside [shortcodeA]content A [/shortcodeA]. While if button B is selected, all the same CPT of the website will display the content of SC b.
I've started using custom fields to store the data of the button, but the first problem is that there is not a button in the custom fields. Should I go instead with a public variable in PHP? Once this is set how to proceed?
thanks

#336498

Something like this

$A= get_post_meta($post->ID, 'type', true);
if $value=="A"{
echo 'here place the content inside shortcode A'
etc...

Hope makes it more clear.

#336549

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Can you please explain that what kind of fields you are using to separate data for A and B? Also, from a logical perspective, how are you recognising a certain custom field, storing the data for A or B?

Since custom fields have no such logical variations, you will have to adopt a method to identify the difference. So you can decide what needs to go through the A, while other goes by B.

Please let me know, thanks.

#336595

>Can you please explain that what kind of fields you are using to separate data for A and B?
Im using a "select".
Im using this code and changing the value to the custom field with jquery and 2 buttons (switch 1 - 2).
I would like to understand how to do this with Views. So is there a way to change the custom field in the frontend (possibly using a button and not the select menu). Then I could use the toolset api instead of a direct call to the db to take wpcf-select.

function select ($atts,$content) {
    global $wpdb;
    $select = $wpdb->get_var( 
        "
        SELECT meta_value 
        FROM $wpdb->postmeta 
        WHERE meta_key = 'wpcf-select'
        " 
    );
    if($select=='1') {		//show content
        return "<span style='font-weight:bold;'>".$content."</span>";
	}
    //otherwise not
    return;
}
add_shortcode( 'my-shortcode', 'select' );

thanks

#336834

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for your feedback. Yes you can grab the value of the custom field on front-end using Types short code, Types API function for custom PHP code or using your own short code (as you did). However, for your short code, you can optimise your code a bit more. Please consider revised code below:

function select ($atts, $content) {
    $post_id = $atts["post-id"];
    $select = get_post_meta($post_id, "wpcf-select", true);

    if($select=='1') {      //show content
        return "<span style='font-weight:bold;'>".$content."</span>";
    }

    //otherwise not
    return;
}
add_shortcode( 'my-shortcode', 'select' );

And use your short code as:

[my-shortcode post-id="1"]

The get_post_meta() is a native function of WordPress API and is used to retrieve the value of custom field, for a specified post. Post ID is a required parameter, so you can pass the post ID via a short code attribute. However, there are some discussions around getting a post ID within a loop or outside the loop, please see followings:

- http://stackoverflow.com/questions/4893435/getting-the-wordpress-post-id-of-current-post
- http://stackoverflow.com/questions/3127385/wordpress-get-the-page-id-outside-the-loop

For information on get_post_meta() please see https://developer.wordpress.org/reference/functions/get_post_meta/

To use Types Short Codes or Types API please consider reading https://toolset.com/documentation/customizing-sites-using-php/functions/#select (remember to click +More icon under the field to expand more information).

#336958
Screen Shot 2015-09-29 at 15.24.41.png

Hello Waqas

thanks for your suggestion about get_post_meta(). I think I need to change a bit the code to achieve what I need though.
Here is the code and the logic with an example:

There is a control panel page (ID 118) with 2 buttons: Day and Night.
Once the user press Day, via jquery, we update wpcf-select with the value Day.
Now, navigating the rest of the web site, only content inside [shortcode time="day"] will show. Of course if the user will press Night only content inside the night shortcode will be shown across the web site.

$control_panel_post = 118;
function update_cf() {
    global $control_panel_post;
    //update this variable via jquery// checkout if types offers an internal function
    $update_cf = 'day';
    update_post_meta( $control_panel_post, 'wpcf-select', $update_cf);
}
add_action( 'loop_start', 'update_cf' );

function select ($atts,$content) {
    global $control_panel_post;
    $time = $atts["time"];
    $cf = get_post_meta($control_panel_post, 'wpcf-select', true);
    if($cf==$time) {
        return $content;
	}
    else {
        return null;
    }
}
add_shortcode( 'my-shortcode', 'select' );

Now before going to make the code to send the value via jquery is there a method to set it on the frontend like in the backend of the picture attached? Also its not clear to me how Types is working here. I am able to update the custom field only using update_post_meta(118, 'wpcf-select', $update_cf);. The backend menu (in the attached image) is not working even if Im on the same post ID. While on the db I see some entries created automatically.

#336997
Screen Shot 2015-09-29 at 17.47.33.png

Ok I coded the jquery part so here is a step by step guide in case someone else could need it.

-- step 1: create a couple HTML buttons

<input type="button" id="day_button" value="Day"/>
<input type="button" id="night_button" value="Night"/>

and create a types custom field named "select"

-- step 2: create a myScript.js file into wp-content/uploads/js/

jQuery(document).ready(function ($j) {
        $j('#day_button').on('click', function() {
            var data = {
                action: 'time',
                time_var: 'day'
            };
	 	$j.post(ajax_var.ajaxurl, data, function(response) {
                        //reloading in case the shortcode is placed on the same page of the buttons
			location.reload();
        });
	 	return false;
        });
        $j('#night_button').on('click', function() {
            var data = {
                action: 'time',
                time_var: 'night'
            };
	 	$j.post(ajax_var.ajaxurl, data, function(response) {
                       location.reload();
        });
	 	return false;
        });
});

-- step 3: into functions.php

//Place the ID of the page where you want to store the Control Panel custom field
$control_panel_post = 118;
//in case you need to load it dynamically
//$control_panel_post = get_the_ID();

//path to myScript
function JSPath() {
    $upload_dir = wp_upload_dir();
    $upload_js_dir = $upload_dir['baseurl'] . '/js/';
    $FCEdgeJS = $upload_js_dir . 'myScript.js';
    return $FCEdgeJS;
}

//localise jquery
function communicate_to_jquery() {
    wp_enqueue_script('jquery'); 
    wp_enqueue_script( 'ajaxTime', JSPath() , array('jquery'),'1.0' , true);
    wp_localize_script( 'ajaxTime', 'ajax_var', array(
        'ajaxurl' => admin_url( 'admin-ajax.php'), 
        )
    );
}
add_action( 'wp_enqueue_scripts', 'communicate_to_jquery' );

//update wpcf-select with jquery
function ajax_time(){
    global $control_panel_post;
    $update_cf = $_POST['time_var'];
    update_post_meta($control_panel_post, 'wpcf-select', $update_cf);
    wp_die(); 
}
add_action( 'wp_ajax_time', 'ajax_time' );

//display the content of the appropriate shortcode
function select ($atts,$content) {
    global $control_panel_post;
    $time = $atts["time"];
    $cf = get_post_meta($control_panel_post, 'wpcf-select', true);
    //var_dump ($cf);
    if($cf==$time) {
        return $content;
	}
    else {
        return null;
    }
}
add_shortcode( 'my-shortcode', 'select' );

on the page you have to use this shortcodes
[my-shortcode time="day"]Day Text[/my-shortcode]
[my-shortcode time="night"]Night Text[/my-shortcode]
___
About Types not updating the custom field from the backend I suspect it is caching the value somewhere, may be there is an option to prevent this? The code is working, but is there a better way to achieve the same result with internal Types api?

#337097

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Types does not cache these things, since it is supposed to deal with CrUD operations. However, I am not sure what may be the actual reason behind this.

Can I ask for a temporary access to your site? So I can look for more details.

I have enabled your next reply as private, please input all details in that area. Please mention the links to the pages, views, forms, CPTs and configurations in question.

Please take a backup of your site, before proceeding.

#337262

I see there is a new feature
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

Is it possible to use Views to achieve the same?

#337302

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Yes, you can use [wpv-conditional] to do evaluations based on custom fields or several other parameters. But to update something in the backend, you will need the custom code (i.e. update_post_meta).

Please let me know if Views seem to resolve your issue otherwise I can look into your site (using the access info you provided).

#337386

Yes of course I will cut down just the "select" function of the above code.
Now I am trying to use a custom user field instead of a custom post field to display the change only to that specific user, but I didn't find the user field using the Views IF. Is it possible?

The problem still persists. I tried deleting a "default" value, but still the backend panel doesn't get updated when the value is changed in the database (via jquery). This can lead to potential conflicts. Could you take a look about why this is happening? From where the types backend panel is taking the value?

Post note.
It would be a killer feature if you can "create" all the ajax code just using Views and the toolset.

#337485

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Alright, I will take a look. Please allow me some time to work on this. I will update you as soon as I find a solution.

#337590
Screen Shot 2015-10-02 at 17.30.10.png

Sure take your time.

What about the first question? Is it possible to load Types custom users fields with Views? Using "Data Origin" -> Types Field I do only see custom fields.

#337871

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Yes it doesn't load users custom fields, but you can manually use the following short code:

[types usermeta="my-field"][/types]

Please see https://toolset.com/documentation/customizing-sites-using-php/functions/ for more information.

P.S.: I am working on your other issue, and will update you shortly on that.

#337873

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

I think the other matter is also resolved. I couldn't figure out that where Day/Night/FF are attached, but yes I was able to check Age (Dark, Iron) however.

There was a little confusion in the short code used on the page (118). It was using user_is_author attribute, which means this is shown from the profile of the author of this page. While you needed to display the currently logged in user, as below:

[types usermeta="age" user_current="true"][/types]

Now I can see it working fine. If I press "Dark" button, it updates the Age field in my profile with correct value, and the same is with "Iron" button. I think you might want to consult with Types fields API around this. Please see https://toolset.com/documentation/customizing-sites-using-php/functions/#select for more information.