0

I'm trying to figure out how to create a button in the WP Text Editor and TinyMCE Editor to trigger the following shortcode.

It's a shortcode to create columns (connected with CSS Grid Foundation), so the user just has to insert numbers value to make different responsive widths (for example: 25% = 3, 50% = 6, 100% = 12, etc.).

Here is the PHP code for the shortcode (in my function.php file):

function my_column( $atts, $content ) {
  $atts = shortcode_atts( array(
    'small' => 12,
    'medium' => null,
    'large' => null
  ), $atts );

  $atts['medium'] = ( $atts['medium'] == null ) ? $atts['small'] : $atts['medium'];
  $atts['large'] = ( $atts['large'] == null ) ? $atts['medium'] : $atts['large'];

  extract( $atts );

  $sizes = 'small-' . $small . ' medium-' . $medium . ' large-' . $large;

  return '<div class="columns ' . $sizes . '">' . do_shortcode( $content ) . '</div>';
}
add_shortcode( 'column', 'my_column');

The shortcode looks like this when you write it (example with 4 columns):

[column medium="6" large="3"] Column 1 [/column]
[column medium="6" large="3"] Column 2 [/column]
[column medium="6" large="3"] Column 3 [/column]
[column medium="6" large="3"] Column 4 [/column]

The idea would be when you click the button to insert the shortcode, it should look like this:

[column medium="" large=""] Column 1 [/column]

I've been looking for a lot of tutorials but I haven't found anything that could really help me with that.

Thank you!

  • Thanks for the help @mmm but this is not what I'm looking for. I would prefer to avoid plugins. – Mathieu Préaud Jun 26 '18 at 12:54
  • ? I don't understand, where do you create the shortcode if it's not in a plugin ? – mmm Jun 26 '18 at 13:01
  • The PHP code I provided comes from the file function.php of my theme. – Mathieu Préaud Jun 26 '18 at 13:04
  • it's a bad idea to put this kind of code in the theme. then broken for broken, you can put the code of my link at the same place. – mmm Jun 26 '18 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.