0

I've created a code snippet with its shortcode to hide part of the post content for non-logged users: [private] ... [/private]

works great but when I try to put another shortcode (contact form 7) inside its not recognized. What could be the problem?

This is my code snippet:


function bp_contenido_privado( $atts, $content = null ) {
   if ( is_user_logged_in() )
   return $content;
   return '<p style="font-weight:bold;padding-top:10px;">Este contenido esta reservado para los usuarios registrados. Registrate <a href="'.wp_registration_url().'">aquí</a> o <a href="'.wp_login_url(get_permalink()).'">inicia la sesión</a> para poder verlo..</p>';
}

and this is the post content:

[privado]
<h4>Por favor llene los datos solicitados. Los formatos aceptados para subir archivos son pdf y jpg con un tamaño máximo de 2mb.</h4>
[contact-form-7 id="3584" title="Formulario de Inscripción"]
[/privado]
5
  • Can you please post your shortcode function? Also, post how you are trying to pass the shortcode.
    – disinfor
    Mar 23, 2020 at 15:19
  • already updated the question
    – ominem
    Mar 23, 2020 at 15:33
  • I'm assuming you are seeing the shortcode code and not what you are expecting. What you can do to test is wrap $content in return do_shortcode( $content ); As a side note, I would strongly recommend wrapping your if statement in {}. Much easier to read.
    – disinfor
    Mar 23, 2020 at 15:51
  • 1
    It worked perfectly. Thank you.
    – ominem
    Mar 24, 2020 at 8:41
  • I'll post as an answer for others.
    – disinfor
    Mar 24, 2020 at 11:49

1 Answer 1

0

You can wrap the entire return output in a do_shortcode(). That will render the shortcode as well as output the regular content.

function bp_contenido_privado( $atts, $content = null ) {
   if ( is_user_logged_in() ) {
      return do_shortcode( $content );
   }
   return '<p style="font-weight:bold;padding-top:10px;">Este contenido esta reservado para los usuarios registrados. Registrate <a href="'.wp_registration_url().'">aquí</a> o <a href="'.wp_login_url(get_permalink()).'">inicia la sesión</a> para poder verlo..</p>';
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.