i have a shortcode from a plugin which i cant modify... This shortcode has some arguments ex. [some_shortcode value=""] - I tried to input the value from post meta as argument for this shortcode, but its not working - here is the code...
This is the code from shortcode i created ( it returns values from post meta )
function test_shortcode( $string ) {
extract( shortcode_atts( array(
'string' => 'string'
), $string));
// check what type user entered
switch ( $string ) {
case 'first':
return get_post_meta( get_the_ID(), 'post_meta_one', true );
break;
case 'second':
return get_post_meta( get_the_ID(), 'post_meta_two', true );
break;
}
}
add_shortcode('test', 'test_shortcode');
Now i want to insert this shortcode in the existing shortcode from the plugin on my page.
For example: [some_shortcode value='[test string="first"]']
It isnt working like this. Thanks for help!
It will not work to insert the shortcode in the existing shortcode like you provided. Your shortcode should have opportunity to process the provided shortcode as attribute.
You should to use the do_shortcode() in your shortcode. You have
[some_shortcode value='[test string="first"]']
and want to use returned value of [test string="first"] which is first in your shortcode. Your code will be:
function some_shortcode($atts){
$atts = shortcode_atts(array(
'value' => ''
), $atts);
$second_shortcode_value = do_shortcode($atts['value']);
//some code
return $something;
}
add_shortcode('some_shortcode', 'some_shortcode');
The variable $second_shortcode_value will containt the output of the [test string="first"] shortcode.
P.S. Avoid of using exctract() function just because it can make your code hard readable.
EDIT:
Here is solution dinamically add attributes to [some_shortcode] value attribute.
function my_shortcode($atts){
$atts = shortcode_atts(array(
'str' => ''
), $atts);
switch ( $atts['str'] ) {
case 'first':
$modified = get_post_meta( get_the_ID(), 'leweb_gender', true );
break;
default:
$modified = '';
}
if(!empty($modified)) {
$second_shortcode_with_value = do_shortcode("[some_shortcode value='$modified']");
}else{
$second_shortcode_with_value = do_shortcode('[some_shortcode]');
}
return $second_shortcode_with_value;
}
add_shortcode('some_shrt', 'my_shortcode');
What we are doing: instead of calling [some_shortcode value='something'] we are generating something into our shortcode and getting content like
[some_shrt str="first"]
Related
I want to "override" the default "gallery" shortcode (WordPress), but only if I have used a given parameter to that gallery shortcode.
For example:
[gallery ids="1,2,3"]
It has no parameter, so it will output the standard gallery code.
[gallery mode="custom" ids="1,2,3"]
It has my "mode" parameter, so it will output another shortcode.
To achieve it, I have created a "gallery" shortcode in functions.php file:
function get_new_gallery( $atts ) {
extract( shortcode_atts( array(
'mode' => '',
'ids' => '',
), $atts ) );
$code ="";
if ($mode == "custom") {
//* Output custom shortcode
$code = '[custom_gallery ids="' . $ids . '"]';
} else {
//* Need to do nothing...but don't know how to do it
$code = '[gallery ids="' . $ids . '"]'; /* Here's the problem, it causes a loop */
}
return do_shortcode($code);
}
add_shortcode( 'gallery', 'get_new_gallery' );
It works fine when I use the mode="custom" parameter. It just output the new shortcode: [custom_gallery...]
However, when not using the parameter it breaks because it enters in an infinite loop. In the code, there's a comment with the line that breaks it.
What I want is to execute the standard "gallery" shortcode if no parameter is entered. But given I've overwritten it...don't know how to "scape" from the loop and just execute the gallery.
Any help?
Thanks in advance.
Maybe an alternative approach can help? What about a filter on the gallery shortcode. See references:
https://codex.wordpress.org/Plugin_API/Filter_Reference/post_gallery
and:
https://wpbeaches.com/filtering-gallery-image-output-wordpress/
I have a shortcode that generates a gallery, given the gallery ID.
function rb_scroll_gallery_shortcode( $atts, $content ) {
$a = shortcode_atts( array(
'id' => -1,
), $atts );
$gallery_ID = $a['id'];
$output = '';
if($gallery_ID != -1){
ob_start();
$gallery = new RB_Scroll_Gallery($gallery_ID);
$gallery->render();
$output = ob_get_clean();
}
return $output;
}
add_shortcode( 'rb_scroll_gallery', 'rb_scroll_gallery_shortcode' );
Now, I've made a Gutenberg block that works perfectly in the editor. You can select a gallery and it will save. However, I dont want to repeat code and have the html in the save property and in the php code.
So I was wondering if there is a way to use that same rb_scroll_gallery_shortcode function to render the block in the front end.
I've seen that you can use register_block_type and set the render_callback to rb_scroll_gallery_shortcode, but I need the ID selected in the block to send it to the function in the $atts array
//This uses the shortcode funtion, but doesn't gives the gallery ID
register_block_type( 'cgb/block-rb-scroll-gallery-block', array(
'render_callback' => 'rb_scroll_gallery_shortcode',
) );
You can try to Convert a Shortcode to a Gutenberg Block and after use in your theme,
Registering the Dynamic Block Callback in PHP
/**
* Register the GitHub Gist shortcode
*/
function gggb_init() {
add_shortcode( 'github-gist', 'gggb_render_shortcode' );
register_block_type( 'github-gist-gutenberg-block/github-gist', array(
'render_callback' => 'gggb_render_shortcode',
) );
}
add_action( 'init', 'gggb_init' );
When your block is rendered on the frontend, it will be processed by your render callback:
function gggb_render_shortcode( $atts ) {
if ( empty( $atts['url'] )
|| 'gist.github.com' !== parse_url( $atts['url'], PHP_URL_HOST ) ) {
return '';
}
return sprintf(
'<script src="%s"></script>',
esc_url( rtrim( $atts['url'], '/' ) . '.js' )
);
}
**Note:** this render callback is intentionally different than the Gutenberg block’s edit callback. Our preference is to use GitHub’s provided JavaScript embed code because this lets GitHub change the embed’s behavior at a future date without requiring a developer to make changes.
Refer link for get more information, https://pantheon.io/blog/how-convert-shortcode-gutenberg-block
I've found out the little thing that messed up my code. The problem wasn't that the render_callback() wasn't getting any attributes (though it wasn't), but it was that the editor wasn't saving them because I forgot to remove some testing data from the attribute galleryID
In the registerBlockType:
The save() method should return null.
The attribute should not have a selector data, since it is used to find the value on the markup return by the save(), wich in this case returns null. I've forgot to remove this data, thats why the attribute wasn't being saved.
attributes: {
galleryID: {
type: 'string',
//This data should only be set if the value can be found in the markup return by save().
//Since save() returns null, we let it out
/*source: 'attribute',
/*attribute: 'data-gallery-id',
/*selector: '.rb-scroll-gallery-holder',*/
},
}
I'm doing a basic shortcode for translating a text in the footer of a wordpress site, and I'm using the get_post_meta() for identifing the language in a meta tag (which I'm adding with a plug in for inserting html code in the header) but it returns empty I'm wondering if this is because the plugin and the order in which he creates the elements(first he executes my shortcode and then the plugin) or if it is something else.
function text_Footer($atts, $content=null){
extract(shortcode_atts(array(
'id' => ''
), $atts));
$ID = get_the_ID();
$lang = get_post_meta(ID,'language',true);
if($lang == 'portuguese')
{
$output='<p>Text in portuguese</p>';
}
else
{
//echo $lang."nope";
$output = '<p>Text in spanish</p>';
}
return $output;
}
get_the_ID() function must be within The Loop.
if you want to extract id from shortcode, just need to use it in write way: $lang = get_post_meta($id,'language',true);
I have created a custom post type "cinfo" and removed title and editor form the edit page. With the help of this code. Also displayed some custom meta fields which are relevant to my plugin.
function remove_box(){
remove_post_type_support('cinfo', 'title');
remove_post_type_support('cinfo', 'editor');
}
add_action("admin_init", "remove_box");
It looks something like this.
Now when i see the list page I still see the title with edit, view and delete button beneath it. which I don't want because the title field doesn't exist in the edit page So it looks a bit irrelevant in the listing page. Instead of that I tried to display the custom meta field "email" but I was only able to change the heading of the column. which looks something like this.
I just did some research and found one action and filter but they still didn't seems to be much of a help to me. Still for the better view of the problem. Also I tried to use a plugin Post List View Count but it also didn't accomplish my purpose. I hope You understand what I basically want to do. Thanks for your time to read my question.
add_filter('manage_cinfo_posts_columns', 'bs_cinfo_table_head');
function bs_cinfo_table_head( $defaults ) {
$defaults['title'] = 'Email';
return $defaults;
}
add_action( 'manage_cinfo_posts_custom_column', 'card_cinfo_table_content', 10, 2 );
function card_cinfo_table_content( $column_name, $post_id ) {
if ($column_name == 'title') {
echo "its working";
}
}
The action manage_cinfo_posts_custom_column will never be true. It's better to remove the defaults on manage_cinfo_posts_columns and do the regular custom stuff in the other filter.
I tried this with a fast class to test all together inside my setup:
class SO23467344
{
private $cpt = 'portfolio';
private $custom_field = 'video';
public function __construct()
{
add_filter( "manage_edit-{$this->cpt}_columns", array( $this, 'column_register' ), 20, 1 );
add_action( "manage_{$this->cpt}_posts_custom_column", array( $this, 'column_display' ), 20, 2 );
add_action( "admin_init", array( $this, "remove_box" ) );
}
function column_register( $columns )
{
$columns['my-col'] = 'My column';
# Remove default columns
unset( $columns['title'], $columns['categories'], $columns['comments'], $columns['date'] );
return $columns;
}
function column_display( $column_name, $post_id )
{
if ( 'my-col' != $column_name )
return;
if ( $field = get_post_meta( $post_id, $this->custom_field, true ) )
echo '<br/><strong style="color:#0f0;font-size:4em"> • </strong>';
}
function remove_box(){
remove_post_type_support( $this->cpt, 'title' );
remove_post_type_support( $this->cpt, 'editor' );
}
}
new SO23467344;
I'm trying to get a value from a ShortCode into a variable for use it in my template file. How can i do that?
Here is my code :
In the post, the short code :
[reference_prix]1-214eddz[/reference_prix]
My plugin code :
$bl_reference_prix = "";
add_shortcode('reference_prix', 'get_blref_prix');
function get_blref_prix( $atts, $reference = null ) {
global $bl_reference_prix;
$bl_reference_prix = $reference;
}
But $bl_reference_prix is still empty.
I've try with $GLOBAL[] but i've the same issu.
What is the best practice for get a value write by the user in a wordpress post and display (or use it) in the template file?
I think the best practice is to use the atts parameter.
// Add Shortcode
function get_blref_prix( $atts ) {
// Attributes
extract( shortcode_atts(
array(
'bl_reference_prix' => '',
), $atts )
);
}
add_shortcode( 'reference_prix', 'get_blref_prix' );
The user of the shortcode will just have to do the following in the editor:
[reference_prix bl_reference_prix="some value by the user"]
And then maybe you can try using the Options API. Add and delete after use.
I've do this and it's working now as :
//Plugin
function get_blref_prix( $atts ) {
global $bl_plugin_refprix, $bl_plugin_refprix_up;
// Attributes
extract( shortcode_atts(
array(
'reference' => '',
'up' => '',
), $atts )
);
$bl_plugin_refprix = $reference;
$bl_plugin_refprix_up = $up;
}
add_shortcode( 'bl_refprix', 'get_blref_prix' );
In the template file (Important : After the function "the_content"!) :
while(have_posts()):the_post();
echo the_content();
endwhile;
echo $bl_plugin_refprix;
In the Post :
[bl_refprix reference="123" up="456"]