phpwordpresswpbakery

how to return vc_link in wp bakery


I am trying to create custom element in wpbakery builder in which i am adding textfield, image and url. Evrything is working fine just there is one issue in url . can anybody please help me out to solve this issue.

Below is the code i am trying :

add_shortcode( 'expert', 'expert_func' );
function expert_func( $atts, $content = null ) { // New function parameter $content is added!
extract(
    shortcode_atts(
      array(
        'expert_image' => 'expert_image',
        'title'   => '',
      ),
      $atts
    )
  );

  $img_url = wp_get_attachment_image_src( $expert_image, "full");

  extract(
    shortcode_atts(
      array(
        'expert_exp_image' => 'expert_exp_image',
        'title'   => '',
      ),
      $atts
    )
  );

  $exp_img_url = wp_get_attachment_image_src( $expert_exp_image, "full");



$result .='<div class="inner__card">
            <div class="expert-img">
              <img src="'. $img_url[0] .'">
            </div>
            <div class="expert-details">
              <p class="name">'.$atts['expert_name'].'</p>
              <p class="designation">'.$atts['expert_designation'].'</p>
              <div class="expect-experience">
                <div class="expect-experience-no">
                  <img src="'. $exp_img_url[0] .'" alt="experience-10">
                </div>
                <div class="expect-experience-text">
                  <p>'.$atts['expert_short_desc'].'</span></p>
                </div>
              </div>
              <p class="details">'.$atts['expert_description'].'</p>
              <div class="expert-btn">';
if( !empty($url) ){
      $column_link_array = vc_build_link($url);
      $url = $column_link_array['url'];
      $result .= "<a href='$url' class='learn-btn'></a>";
  }
$result .=' </div>
            </div>
        </div>';

 //$result = $button;

 //print_r($atts);

 return $result;

 }

 //backend
 add_action( 'vc_before_init', 'expert_integrateWithVC' );
 function expert_integrateWithVC() {
 vc_map( array(
"name" => __( "Expert", "my-text-domain" ),
"base" => "expert",
"class" => "",
"category" => __( "Custom", "my-text-domain"),
"params" => array(
array(
"type" => "attach_image",
"holder" => "div",
"class" => "",
"heading" => __( "Choose Expert Image", "my-text-domain" ),
"param_name" => "expert_image",
"value" => __( "", "my-text-domain" )

),
array(
"type" => "textfield",
"holder" => "div",
"class" => "",
"heading" => __( "Expert Name", "my-text-domain" ),
"param_name" => "expert_name",
"value" => __( "", "my-text-domain" )
 ),
array(
"type" => "textfield",
"holder" => "div",
"class" => "",
"heading" => __( "Expert Designation", "my-text-domain" ),
"param_name" => "expert_designation",
"value" => __( "", "my-text-domain" )
),
array(
"type" => "attach_image",
"holder" => "div",
"class" => "",
"heading" => __( "Add Year of Expirience image", "my-text-domain" ),
"param_name" => "expert_exp_image",
"value" => __( "", "my-text-domain" )
 ),
 array(
 "type" => "textfield",
 "holder" => "div",
 "class" => "",
 "heading" => __( "Expert's Expirience Short Description", "my-text-domain" ),
 "param_name" => "expert_short_desc",
 "value" => __( "", "my-text-domain" )
 ),
 array(
"type" => "textarea",
"holder" => "div",
"class" => "",
"heading" => __( "Short Description of Expert", "my-text-domain" ),
"param_name" => "expert_description",
"value" => __( "", "my-text-domain" )
),
 array(
 "type" => "vc_link",
 "holder" => "div",
 "class" => "",
 "heading" => __( "Choose Rediect URL", "my-text-domain" ),
 "param_name" => "url",
 "value" => __( "", "my-text-domain" ),
 "description" => __( "Add Short Description", "my-text-domain" )
 )

 )
 ) );
 }

All i want is to return URL selected. i am new in wordpress. Thank you in advance.


Solution

  • You are not extracting your URL so if( !empty($url) ) { wont fire, you should add all your variables to your extract code.

    function expert_func( $atts ) {
    
      extract( shortcode_atts( array(
        'expert_image'  => '',
        'expert_name'  => '',
        'expert_designation' => '',
        'expert_exp_image' => '',
        'expert_short_desc' => '',
        'expert_description'  => '',
        'url' => '',
      ), $atts ) );
    
      $img_url = wp_get_attachment_image_src( $expert_image, "full");
    
      $url_link = vc_build_link($url);
    
      your code...
    
      if (isset($url_link['url'])) {
    
        $result .= '<a href="'.esc_url($url_link['url']).'" class="learn-btn">'.$url_link['title'].'</a>';
    
      }
    
      your code etc...
    
    }