Shortcode question

stockamt200
Posts: 3
Joined: Sun Jun 14, 2020 2:14 pm

Shortcode question

Post by stockamt200 »

Hello,

I was checking if it was possible to use shortcode to post part of a post from phpbb into wordpress. I know https://www.axew3.com/w3/2017/07/wordpr ... o-wp-post/ shows to post the complete post but if I only wanted say 50 or 100 words of the post to show on the front page can I do that with a short code? I tried text_words="XXX" but I guess that option only works with topics.

Also, is is possible to show the a link to the post back into phpbb, similar to how the latest topics works so that someone can click on the post and it takes them back to the forums.

Thanks,
User avatar
axew3
w3all User
w3all User
Posts: 2689
Joined: Fri Jan 22, 2016 5:15 pm
Location: Italy
Contact:

Re: Shortcode question

Post by axew3 »

First question clear, the second not, because my answer for what i have understand would be: why you do not add a link to the post then? So i have not understand the question i assume.

And assuming you know how to edit a php file, use ftp etc, the short answer to the first question is:
into file
/wp-content/plugins/wp-w3all-phpbb-integration/class.wp.w3all-phpbb.php

there is this function:

Code: Select all

// wp_w3all_get_phpbb_post_short Version 1.0
// This need to be rewrite/improved: all should be done following the [code][code] logic ...
public static function wp_w3all_get_phpbb_post_short( $atts ) {
	global $w3all_config;
	$w3db_conn = self::w3all_db_connect();
	
    $p = shortcode_atts( array(
        'id' => '0', 
        'plaintext' => '0', 
    ), $atts );
    
$p['id'] = intval($p['id']);
if($p['id'] == 0){
	return "w3all shortcode error.<br /> The shortcode need to be added like this:<br />[w3allforumpost id=\"150\"]<br />change '150' <strong>with the (existent) phpBB post ID to display here.</strong>"; 
}

$phpbb_post = $w3db_conn->get_results("SELECT T.*, P.* FROM ".$w3all_config["table_prefix"]."topics AS T, ".$w3all_config["table_prefix"]."posts AS P 
  WHERE T.topic_visibility = 1 
   AND T.topic_id = P.topic_id 
   AND P.post_visibility = 1 
   AND P.post_id = '".$p['id']."'
   ");

if( !$phpbb_post ){
	$res = '<b>w3all shortcode error:<br />the provided post ID to show do not match an existent phpBB post!</b>';
	return $res;
}
   
$p['plaintext'] = intval($p['plaintext']);
if($p['plaintext'] == 1){
	return preg_replace('/[[\/\!]*?[^\[\]]*?]/', '', $phpbb_post[0]->post_text); // REVIEW // remove all bbcode tags (not html nor fake tags) //
}

// handle bbcode in the right way ...
// grab the code and replace with a placeholder '#w3#bbcode#replace#' 
// so, after the others bbcode tags conversions, re-add each, properly wrapped 
preg_match_all('~\<s\>\[code\]\</s\>(.*?)\<e\>\[/code\]\</e\>~si', $phpbb_post[0]->post_text, $cmatches, PREG_SET_ORDER);
if($cmatches){ // remove and add custom placeholder
$cc = 0;
$phpbb_post[0]->post_text = preg_replace('~\<s\>\[code\]\</s\>(.*?)\<e\>\[/code\]\</e\>~si', '#w3#bbcode#replace#', $phpbb_post[0]->post_text, -1 ,$cc);
// split and add 'placeholders'
$ps = preg_split('/#w3#bbcode#replace#/', $phpbb_post[0]->post_text, -1, PREG_SPLIT_DELIM_CAPTURE);
$ccc = 0;
$res = '';
foreach($ps as $p => $s){
if($ccc < $cc){
 $res .= $s.'#w3#bbcode#replace#'.$ccc++; // append/assing number to placeholder for this split/string
} else { $res .= $s; } // follow add the latest text, if no more placeholders ...
}
} else { $res = $phpbb_post[0]->post_text; }

$res = self::w3all_bbcodeconvert($res); // convert all bbcode tags except [code]

if($cmatches){ // re-add grabbed bbcode blocks and wrap with proper html ...
$cccc = 0;
foreach($cmatches as $k => $v){
$res = str_ireplace('#w3#bbcode#replace#'.$cccc, '<code>'.$v[1].'</code>', $res);
$cccc++;
}
}

return $res;
}
that to achieve what you want should be substituted by this:

Code: Select all

// wp_w3all_get_phpbb_post_short Version 1.0
// This need to be rewrite/improved: all should be done following the [code][code] logic ...
public static function wp_w3all_get_phpbb_post_short( $atts ) {
	global $w3all_config;
	$w3db_conn = self::w3all_db_connect();
	
    $p = shortcode_atts( array(
        'id' => '0', 
        'plaintext' => '0',
        'wordsnum' => '0'
    ), $atts );

$p['id'] = intval($p['id']);
if($p['id'] == 0){
	return "w3all shortcode error.<br /> The shortcode need to be added like this:<br />[w3allforumpost id=\"150\"]<br />change '150' <strong>with the (existent) phpBB post ID to display here.</strong>"; 
}

$phpbb_post = $w3db_conn->get_results("SELECT T.*, P.* FROM ".$w3all_config["table_prefix"]."topics AS T, ".$w3all_config["table_prefix"]."posts AS P 
  WHERE T.topic_visibility = 1 
   AND T.topic_id = P.topic_id 
   AND P.post_visibility = 1 
   AND P.post_id = '".$p['id']."'
   ");

if( !$phpbb_post ){
	$res = '<b>w3all shortcode error:<br />the provided post ID to show do not match an existent phpBB post!</b>';
	return $res;
}
   
if(intval($p['plaintext']) == 1){
	
if($p['wordsnum'] > 0){
 return wp_w3all_remove_bbcode_tags($phpbb_post[0]->post_text, $p['wordsnum']);
}

 return preg_replace('/[[\/\!]*?[^\[\]]*?]/', '', $phpbb_post[0]->post_text); // REVIEW // remove all bbcode tags (not html nor fake tags) //

}

// handle bbcode in the right way ...
// grab the code and replace with a placeholder '#w3#bbcode#replace#' 
// so, after the others bbcode tags conversions, re-add each, properly wrapped 
preg_match_all('~\<s\>\[code\]\</s\>(.*?)\<e\>\[/code\]\</e\>~si', $phpbb_post[0]->post_text, $cmatches, PREG_SET_ORDER);
if($cmatches){ // remove and add custom placeholder
$cc = 0;
$phpbb_post[0]->post_text = preg_replace('~\<s\>\[code\]\</s\>(.*?)\<e\>\[/code\]\</e\>~si', '#w3#bbcode#replace#', $phpbb_post[0]->post_text, -1 ,$cc);
// split and add 'placeholders'
$ps = preg_split('/#w3#bbcode#replace#/', $phpbb_post[0]->post_text, -1, PREG_SPLIT_DELIM_CAPTURE);
$ccc = 0;
$res = '';
foreach($ps as $p0 => $s){
if($ccc < $cc){
 $res .= $s.'#w3#bbcode#replace#'.$ccc++; // append/assing number to placeholder for this split/string
} else { $res .= $s; } // follow add the latest text, if no more placeholders ...
}
} else { $res = $phpbb_post[0]->post_text; }

$res = self::w3all_bbcodeconvert($res); // convert all bbcode tags except [code]

if($p['wordsnum'] > 0){

 $post_s = $res;
 $res0 = explode(' ',$post_s);

  if( count($res0) < $p['wordsnum'] ) : return $post_s; endif;

 $post_std = ''; $i = 0; $b = $p['wordsnum'];
 $res1 = '';
  foreach ($res0 as $post_st) {
	
	  $i++;
	  if( $i < $b + 1 ){ // offset of 1

      $res1 .= $post_st . ' ';
    }
  }
  $res = $res1;
}


if($cmatches){ // re-add grabbed bbcode blocks and wrap with proper html ...
$cccc = 0;
foreach($cmatches as $k => $v){
$res = str_ireplace('#w3#bbcode#replace#'.$cccc, '<code>'.$v[1].'</code>', $res);
$cccc++;
}
}

return $res;
}
now the shortcode have an attribute wordsnum
then for example using the shortcode like this:

Code: Select all

[w3allforumpost id="4" wordsnum="150"]
or like this:

Code: Select all

[w3allforumpost id="4" plaintext="1" wordsnum="150"]
will return you number of words.
May it is not perfect, you'll have to play to adjust correct number of words in certain cases, and avoid truncation of something. By the way it works quite well on short tests i've do on fly, so may it will be added by default into next plugin version
stockamt200
Posts: 3
Joined: Sun Jun 14, 2020 2:14 pm

Re: Shortcode question

Post by stockamt200 »

Thanks.

Yes I guess in the section I could make the title of the shortcode section a clickable URL.

Will make the changes in php.

Thank you once again.
User avatar
axew3
w3all User
w3all User
Posts: 2689
Joined: Fri Jan 22, 2016 5:15 pm
Location: Italy
Contact:

Re: Shortcode question

Post by axew3 »

Let know how it goes if you can, and check that just seconds after posted, the code has been edited in one line moved inside a condition, so check to grab the corrected above at date of this post!
stockamt200
Posts: 3
Joined: Sun Jun 14, 2020 2:14 pm

Re: Shortcode question

Post by stockamt200 »

axew3 wrote: Sun Jun 21, 2020 3:21 pm Let know how it goes if you can, and check that just seconds after posted, the code has been edited in one line moved inside a condition, so check to grab the corrected above at date of this post!
It seems to work fine for me. Will change the code with the edited version as well and see if there is any difference. I might have used the corrected version. Will do a diff first.
Post Reply