Attribute as shortcode

Hola compadre !

I am not sure I can help you here, as this type of coding is new and quite cryptic to me, though I have been developing for two decades now. I am really interested in getting to know how to handle it better though. For the moment, it seems like I am mostly trying to “game the system”.

Incidentally, yesterday I was facing a similar problem.
So I managed to create a shortcode displaying some custom attributes in my custom templates.

Basically, I retrieve the current user id from the context (from the vendor / listings being displayed, not being connected). From there I go down the SQL tree to gain access to the value stored in the database. It may break all the rules implemented by the MVC framework, but it works ! The good old way. Oldies but goldies.

Here’s a sample code :

function get_portfolio_urls(){
	$current_user = wp_get_current_user();
	global $wpdb;
	$attr_name = "hp_url_%"; //my attributes are hp_url_1, hp_url_2 etc...
	
	$query = $wpdb->prepare(
		"
		SELECT pm.meta_value
		FROM (({$wpdb->postmeta} as pm 
		INNER JOIN {$wpdb->posts} as p
		on p.id = pm.post_id) 
		INNER JOIN {$wpdb->users} as u
		on p.post_author = u.id) 
		where pm.meta_key LIKE '". $attr_name ."' and u.id=%d
		",
		$current_user->ID
	);

	// Execute the query
	$results = $wpdb->get_results($query);

	// Check and output results
	if (!empty($results)) {
		$output ="<h2>Mon travail</h2>";
		foreach ($results as $result) {
			//return ;
			$url  = esc_attr($result->meta_value); 
			$output .='<a href="' . $url . '" title="' . $url . '" target="_blank" class="portfolio-url"><img alt="' . $url . '" src="https://lancelo.fr/thumbs/?url='. $url  .'" height="225" width="400"/></a>';	
		}
		return $output; 
	}	
}
add_shortcode('urls', 'get_portfolio_urls'); //use case : [urls]

Conversely, I found out yesterday, that you can use your own shortcode in attribute UI table, which was a great aha moment :

It seems this HivePress framework is quite powerful, but a whole new world for me.For the moment, it’s mostly a trial-and-error / learning-by-doing process.

But the support looks great which is a consolation.

Hope this helps.

1 Like