Skip Navigation

[Resolved] Using own php-function in Views loop editor

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.

This thread is resolved. Here is a description of the problem and solution.

Problem:
I want pass post-fields to own php-function and then display the output in Views loop editor.
What I'm trying to get is function that parses output from 2 different custom fields containing date-time (2017-09-21 11:00:00). I can compare and format them easily in php-function, but not in Views Loop.
How do I pass those two fields to my own php-function and display result in loop?

Solution:
1. How do I pass those two fields to my own php-function and display result in loop?
==> You can make shortcode for your php function and pass those fields in shortcode attributes, like:

 [my_custom_shortcode attr_1="field1_shortcode" attr_2="field2_shortcode"] 

- Then after processing those fields you can return result back to the shortcode for display..
- Here is WP shortcode API: https://codex.wordpress.org/Shortcode_API

2. Client used following php-code into theme’s functions.php file:

function this_is_test_shortcode_func( $atts ) {
    $attlist = shortcode_atts(
        array(
            'first' => '(no first)',
            'second' => '(no second)',
        ), $atts, 'this_is_test_shortcode' );
    return "Output 1: " . $attlist['first'] . " / 2: " . $attlist['second'];
}
add_shortcode( 'this_is_test_shortcode' , 'this_is_test_shortcode_func' );

-- And used shortcode in Loop Output Editor like this:

[this_is_test_shortcode first="[wpv-post-title]" second="[wpv-post-date]"]

It outputs nicely those two fields as a return.

Relevant Documentation:
- https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-custom-functions-in-conditions/

- https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-shortcodes-in-conditions/

This support ticket is created 6 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

Sun Mon Tue Wed Thu Fri Sat
- 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 -
- 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 -

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

This topic contains 2 replies, has 2 voices.

Last updated by jussiP 6 years, 7 months ago.

Assisted by: Noman.

Author
Posts
#572016

I want pass post-fields to own php-function and then display the output in Views loop editor.
What I'm trying to get is function that parses output from 2 different custom fields containing date-time (2017-09-21 11:00:00). I can compare and format them easily in php-funtion, but not in Views Loop.
How do I pass those two fields to my own php-function and display result in loop?

#572053

Noman
Supporter

Languages: English (English )

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

Hi Jussi,

Thank you for contacting Toolset support. This will require some good level programming expertise, below is the approach that you would follow:

1. How do I pass those two fields to my own php-function and display result in loop?
==> You can make shortcode for your php function and pass those fields in shortcode attributes, like:

[my_custom_shortcode attr_1="field1_shortcode" attr_2="field2_shortcode"]

- Then after processing those fields you can return result back to the shortcode for display..

- Here is WP shortcode API: https://codex.wordpress.org/Shortcode_API

2. View also has conditional Shortcode to compare things and you can use your custom functions in conditions, here is the doc:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-custom-functions-in-conditions/

You can also use your own custom shortcodes in Views conditional output:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-shortcodes-in-conditions/

Please note that above info is specially for someone having WP programming experience.
Thank you

#572097

Thank you for guidance. I tested this, and the solution seems to work.

I put following php-code to my themes functions.php:

function this_is_test_shortcode_func( $atts ) {
	$attlist = shortcode_atts(
		array(
			'first' => '(no first)',
			'second' => '(no second)',
		), $atts, 'this_is_test_shortcode' );
	return "Output 1: " . $attlist['first'] . " / 2: " . $attlist['second'];
}
add_shortcode( 'this_is_test_shortcode' , 'this_is_test_shortcode_func' );

And this row to Loop Output Editor:

[this_is_test_shortcode first="[wpv-post-title]" second="[wpv-post-date]"]

It outputs nicely those two fields as a return. Next step is to build the date parser in this php-function.

The forum ‘Types Community Support’ is closed to new topics and replies.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.