Visual Composer: Custom Shortcode Not Working - php

The following is the shortcode I created in functions.php:
function echo_first_name() {
echo $_GET['first_name'];
}
add_shortcode( 'first_name', 'echo_first_name' );
And I'm entering the following into my Visual Composer editor:
['first_name']
This produces no result, even when using the Visual Composer shortcode mapper.
Does anybody know why this isn't working? Do I have to register it as another type of shortcode for Visual Composer to be able to access it?

For Create Short code
if you want to add short code in editor then Used return instead of echo
function echo_first_name() {
return $_GET['first_name'];
}
add_shortcode( 'first_name', 'echo_first_name' );
Used Short Code Like
[first_name]
If you want to pass the value In shortcode
function echo_first_name( $atts ) {
$a = shortcode_atts( array(
'firstname' => '',
), $atts );
return "First Name= {$a['firstname']}";
}
add_shortcode( 'first_name', 'echo_first_name' );
Used Short Code Like
[first_name firstname="test"]

You are passing a $_GET['firstname'] in the shortcode function from where you are passing this in URL or from any other place. Please check it is coming or not.
Or if you want to test your shortcode is working or not use beloww code it will work.
function echo_first_name() {
return 'testing the shortcode';
}
add_shortcode( 'first_name', 'echo_first_name' );

Use return instead of echo
function echo_first_name(){
return $_GET['first_name'];
}
add_shortcode( 'first_name', 'echo_first_name' );

Related

PHP include ability in WP post/pages via shortcode

I have a few .html and .php pages in my custom wordpress plugin directory/files that I'm using to output styles JSON/jQuery data basically.
I am wondering how I could essentially wrap these modules in a short code and insert that short code in my Wordpress posts or pages via the Wordpress wysiwyg editor. i.e. [module 1]. I do not want to do this in the theme's files or /functions.php I want to add this functionality from my plugin files, any thoughts?
So, like a php include in the form of a wordpress short code, that works in Wordpress pages.
Here is what I'm trying; within my main plugin php file:
function my_form_shortcode() {
include dirname( __FILE__ ) . 'https://absolute.path.com/wp-content/plugins/my-plugin-v4/assets/files/2019/results/index.php';
}
add_shortcode( 'my_form_shortcode', 'my_form_shortcode' );
Within my Wordpress page, I do: (although, does not display/work)
[my_form_shortcode]
You can add your shortcode function the same way you would in your theme
add shortcode function in plugin
Simplest example of a shortcode tag using the API: [footag foo="bar"]
function footag_func( $atts ) {
return "foo = {$atts['foo']}";
}
add_shortcode( 'footag', 'footag_func' );
Example with nice attribute defaults: [bartag foo="bar"]
function bartag_func( $atts ) {
$atts = shortcode_atts( array(
'foo' => 'no foo',
'baz' => 'default baz'
), $atts, 'bartag' );
return "foo = {$atts['foo']}";
}
add_shortcode( 'bartag', 'bartag_func' );
Example with enclosed content: [baztag]content[/baztag]
function baztag_func( $atts, $content = "" ) {
return "content = $content";
}
add_shortcode( 'baztag', 'baztag_func' );
If your plugin is designed as a class write as follows:
class MyPlugin {
public static function baztag_func( $atts, $content = "" ) {
return "content = $content";
}
}
add_shortcode( 'baztag', array( 'MyPlugin', 'baztag_func' ) );

Wordpress Shortcode Issus v3

I want to see if him doing this code correctly to developed a custom shortcode for contact form want to create for my website.
I have looked at wp codex want to use class so I came up with this code to if this would work.
<?php
class MyPlugin {
// Conact Form shortcode
public static function allb_contact_form( $atts, $content = " " ) {
//[contact_form]
//get the attribute_escape
$atts = shortcode_atts(
array(),
$atts,
'contact_form'
);
//return HTML
ob_start();
include 'lib/inc/thmeplates/contact-form.php';
return ob_get_clean();
}
}
add_shortcode( 'contact_form', array( $this , 'allb_contact_form' ) );
But it doesn't give me error messages that says. One guy told me to put the add_shortcode( 'contact_form', array( $this , 'allb_contact_form' ) ); at the outside of code.
If you are intent on using a class, wherever you are declaring your plugin add
$myplugin = new MyPlugin();
add_shortcode( 'contact_form', [ $myplugin , 'allb_contact_form' ] );
And remove that add_shortcode from the class. It's not even valid code laying there.

Remove filter of a plugin

I have a review plugin that overrides the comment form in a specific posttype.
Now I'm trying to seperate the reviews and comments.
My first step is to remove the filter that modifies the current comment template and use that filter inside a second comment form.
The plugin uses this code (simplified)
final class DM_Reviews {
public function hooks() {
do_action_ref_array( 'dm_reviews_before_setup_actions', array( &$this ) );
add_filter( 'comment_form_defaults', array( $this, 'reviews_form' ) );
do_action_ref_array( 'dm_reviews_after_setup_actions', array( &$this ) );
}
public function review_form( $args ) {
$form = 'plugin code to modify form';
return wp_parse_args( $form, $args );
}
}
In my child theme's function.php file, I tried to use this but it didn't worked.
global $DM_Reviews;
remove_filter( 'comment_form_defaults', array($DM_Reviews, 'reviews_form'),1 );
WP Codex
If someone can put me in the right direction on how to solve it, it would help me a lot.
I think you can achieve this goal, using one of the following solutions depending on the way this plugin instantiates the class:
if( class_exists('DM_Reviews' ) ){
//This should work in whatever case, not tested
remove_filter('comment_form_defaults', array( 'DM_Reviews', 'reviews_form'));
//or Instantiating a new instance, not tested
remove_filter('comment_form_defaults', array( new DM_Reviews(), 'reviews_form'));
//or Targeting the specific instance, not tested
remove_filter('comment_form_defaults', array( DM_Reviews::get_instance(), 'reviews_form'));
}
Hope it helps, let me know if you get stuck.
for me remove_filter didn't work from function.php i wanted to remove a specific behavior of a plugin so what i did :
add_action( 'init', 'remove_filters' );
function remove_filters(){
global $wp_filter;
unset( $wp_filter["_filter_name"]);
}
Try this :
$instance = DM_Reviews::this();
remove_filter('comment_form_defaults', array( $instance, 'reviews_form'));

WordPress: Changing comment_form(); $defaults in plug-in

I'm using the extend comment plugin to change the comment_form(); drastically. Now I want to control the values of the $default options in the plugin. Tested the PHP below, but got the following error:
php:
add_filter( 'comment_form_defaults', $defaults );
function custom_defaults($defaults) {
$comment_field_hide = '';
$defaults[ 'label_submit' ] = 'fooBar';
return $defaults;
}
EDIT: Changed according to the comment below, error is gone, but function doesn't take effect.
Indeed, the method is "add_filter"... that's the error. Remove the "s"
it seems you do not use parameters the right way too... Second parameter should be the function's name, so change it to :
add_filter( 'comment_form_defaults', 'custom_defaults' );
function custom_defaults($defaults) {
$comment_field_hide = '';
$defaults[ 'label_submit' ] = 'fooBar';
return $defaults;
}

Echo a custom field within a function?

So I have a custom field called 'ingredients'. I want to output this custom field information out onto my custom tab.
This is what I have but it echos out nothing.
function my_custom_tab(){
global $post;
echo get_post_meta($post->ID, 'ingredients', true);
}
Any ideas on why its not echoing anything out?
EDIT: Whatever I put in that last function I need to echo there like this:
add_filter('woocommerce_product_tabs', 'woo_remove_product_tabs', 98);
function woo_remove_product_tabs( $tabs ){
$tabs['my_custom_tab'] = array(
'title' => "My Custom Tab",
'priority' => 15,
'callback' => 'my_custom_tab'
);
return $tabs;
}
function my_custom_tab(){
echo '<h2>This is a title</h2>';
global $post;
return get_post_meta($post->ID, 'ingredients', true);
}
If I insert that echo it will output that in the custom tab. Is there anyway to just say echo 'ingredients', the custom field?
Use return then echo the function.
function my_custom_tab(){
global $post;
return get_post_meta($post->ID, 'ingredients', true);
}
echo my_custom_tab();
return as you can guess, will pass the value back, so when you echo the function it should display the value.
Also make sure that get_post_meta is actually working.
Some background reading:
http://php.net/manual/en/functions.returning-values.php
I figured it out. It was alot simplier than I expected:
function my_custom_tab(){
echo get_post_meta( get_the_ID(), 'ingredients', true );
}

Categories