Skip Navigation

[Resolved] Shortcode in PHP

This support ticket is created 6 years, 11 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 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

Tagged: 

This topic contains 17 replies, has 3 voices.

Last updated by Bochnacki 6 years, 11 months ago.

Assisted by: Luo Yang.

Author
Posts
#527593
<a href="<em><u>hidden link</u></em> format="meta" meta='ID']">[wpv-post-author format="meta" meta="nickname"] </a>
<a href="<em><u>hidden link</u></em> format="meta" meta='ID']">[types usermeta='logo' size='thumbnail' align='none' resize='proportional' user_is_author='true'][/types]</a>

How can I save this code in the functions.php file?
I do not know PHP well and despite many attempts I am not successful.

And this shortcode is a complete puzzle

[wpv-conditional if="( (empty($(_thumbnail_id))) )"]
<a href="[wpv-post-url]"><div class="replace"></div></a>
[/wpv-conditional]

I would be grateful for the help, or at least pointing out how I can add those pieces of code in functions.php.

#527598

I will add that I want to include this in the function in which I show elements in this way:

.....
$price = types_render_field( "price", array( "output" => "html", "post_id"=>$post_id ));

$content .= "<div class='price'>". $price ."</div>;
.....

I do not know if it matters, but it may help to explain what I mean.

#527743

Dear Bochnacki,

There are lots of Types & Views shortcode in your codes, I suggest you store them into a content template:
https://toolset.com/documentation/user-guides/view-templates/

And display the content template with PHP codes using Views function render_view_template():
https://toolset.com/documentation/programmer-reference/views-api/#render_view_template

#527835
correct-view.jpg
bad-view.jpg

I ask how shortcodes save in PHP code form, and you refer me to the tutorial - how to make a template "... using only HTML and no PHP"...

https://toolset.com/documentation/programmer-reference/views-api/#render_view_template is also not helpful, because in this case shows nonsensical view - screen "bad-view.jpg".
This probably means that the view is rendered in a particular environment. This is the code responsible for displaying your favorite posts.
My current code looks like this:

// favorites
function wptypes_listvfav_func($atts=array(), $content=null)
{
extract( shortcode_atts( array(), $atts ) );
if (function_exists('wpfp_get_users_favorites')):
$favorite_post_ids = wpfp_get_users_favorites();
$limit = 10;
$content .= "";
    if ($favorite_post_ids):
    $c = 0;
    $favorite_post_ids = array_reverse($favorite_post_ids);
    foreach ($favorite_post_ids as $post_id) {
    if ($c++ == $limit) break;
    $p = get_post($post_id);
	
$favcena = types_render_field( "cena", array( "output" => "html", "post_id"=>$post_id ));
$opcjeceny = types_render_field( "opcje-ceny", array( "separator" => ", ", "output" => "html", "post_id"=>$post_id ));
$krotkiopis = types_render_field( "krotki-opis", array( "output" => "html", "post_id"=>$post_id ));
$przebieg = types_render_field( "przebieg", array( "format" => "FIELD_VALUE", "output" => "html", "post_id"=>$post_id ));
$rokprodukcji = types_render_field( "rok-produkcji", array( "format" => "FIELD_VALUE", "output" => "html", "post_id"=>$post_id ));
$rodzajpaliwa = types_render_field( "rodzaj-paliwa", array( "output" => "html", "post_id"=>$post_id ));
$pojemnoscsilnika = types_render_field( "pojemnosc-silnika", array( "format" => "FIELD_VALUE", "output" => "html", "post_id"=>$post_id ));
$lokalizacja = types_render_field( "lokalizacja", array( "output" => "html", "post_id"=>$post_id ));
$vendorstatus = get_term_meta( "wpv-post-author", array( "meta" => "ID", "output" => "html", "post_id"=>$post_id ) );

		$content .= "<div class='ad favorites'><div class='ad-thumb'>";
		$content .= get_the_post_thumbnail ( $post_id ). "</div>
			<div class='ad-data'><div class='ad-title'><a href='".get_permalink($post_id)."' title='".  $p->post_title ."'><h2>".$p->post_title."</h2></a></div><div class='ad-price'>". $favcena ."</div><div class='price-note'>". $opcjeceny ."</div><div class='ad-items'><div class='ad-items-left'><div class='ad-desc'>". $krotkiopis ."</div><div class='ad-item-milage'>". $przebieg ."</div>". $vendorstatus."<div class='ad-item-year'>". $rokprodukcji ."</div><div class='ad-item-fuel'>". $rodzajpaliwa ."</div><div class='ad-item-capacity'>". $pojemnoscsilnika ."</div><div class='ad-item-location'>". $lokalizacja ."</div></div><div class='ad-items-right'>
			<div class='cont-form-pop popup'>";
		$content .= "</div><a class='ad-item-delete' href='?wpfpaction=remove&page=1&postid=$post_id'>Usuń z Ulubionych</a></div></div></div></div>";
    }
    else:
    $content .= "";
    $content .= "<h4 class='fav-empty'>Tu jest miejsce na Twoje ulubione ogłoszenia</h4>";
    $content .= "";
    endif;
    $content .= "";
endif;
return $content;
}
add_shortcode('wptypes_listvfav', 'wptypes_listvfav_func');

"correct-view.jpg" shows what I've done so far.
I need some more items that I wrote in the beginning, but I do not know how to save them in PHP.

#527906

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello Bochnacki - Luo is on vacation. This is Minesh here and I'll take care of this ticket.

Well - we have number of Docs available that will guide you how you can customize your site using Toolset with PHP:
=> https://toolset.com/documentation/customizing-sites-using-php/

I suggest you follow the guidance given with above doc and try to resolve your issue. If you need further assistance, please feel free to contact me here 🙂

#529372

Minesh, thanks for the hint.
I have not had time to do this yet. I will try to take care of this and let you know.

#529460

Unfortunately, my PHP knowledge is too weak for me to use good examples from the link you provided.
I can not even put [wpv-post-author format = "meta" meta = "nickname"] into my code, which I showed above ...

#529609

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

- Could you please tell me what information you want to display and where?
- Please share problem URL?

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#529853

Oh, I see that in pasted shortcodes [] (square brackets) it changes to

[  ]

and I can not change it.
I hope this will not be a problem for you.

#530409

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - for this page:
hidden link

Could you please tell me which template are you using and path of this PHP template file?

#530436

If you are asking for a WordPress template, then I use "Auto-box", my own template.
If you ask how I display "favorite", then I'm using the "Ulubione" layout ( hidden link ) that is assigned to the "Ulubione" page. But there is only a shortcode [wptypes_listvfav][/wptypes_listvfav], and the PHP (this shortcode) code showing the positions on "Ulubione" is in the functions.php file.

I hope it will help 🙂

#530449

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Yes - it helps a lot to understand the things. Thanks a lot.

Now, I've another question is that the favorite posts belong to CPT "Osobowe" only or you can make any post type as the favorite?

#530497

All CPT: "Osobowe", "Dostawcze",...
There are no views yet for all CTP, but the items/fields I want to show in 'favorites' will be the same as in "Osobowe".

#530550

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Could you please check now.

I've created the view: "display-favorites" - this view have same code as you wanted - if you want, you can change/delete/modify the view output as you wanted.
=> hidden link

I've added folloing code to your functions.php file:

function search_filter($query) {
   global $WP_Views;
  
  if ( !is_admin() ) {
	  
    if($WP_Views->current_view==1753) {
		if (function_exists('wpfp_get_users_favorites')){
			$favorite_post_ids = wpfp_get_users_favorites();
			$favorite_post_ids = array_reverse($favorite_post_ids);
			$query->set('post__in',  $favorite_post_ids);
			$query->set( 'order', 'ASC' );
			
		}
   }
  }
}

And I can see the same output so no need to add/use the custom shortcode you created.

So - I've added the view to layout:
=> hidden link

You can delete the unwanted shortcode you added to layout now.

#530603

Well ... Thanks for the help, but that's not what I meant.
It does not work like "favorite".
You can not add a post to a "favorite" or delete it from a "favorite". This is simply a copy of posts from the category (in this case, "osobowe").

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