Automattic / WP-Job-Manager

Manage job listings from the WordPress admin panel, and allow users to post jobs directly to your site.
https://wpjobmanager.com
GNU General Public License v3.0
901 stars 368 forks source link

"Updating failed" after adding a custom shortcode #1662

Open antoinelbch opened 5 years ago

antoinelbch commented 5 years ago

Steps to Reproduce

  1. Add categories shorcode: https://wpjobmanager.com/customization-snippets/#displayCat
  2. Add to WP5 page shorcode block: [list_categories]
  3. Publishing gives error: "Updating failed"

Reason

The function called by the shortcode should never produce output of any kind. Shortcode functions should return the text that is to be used to replace the shortcode.

Solution

Please see: https://github.com/WordPress/gutenberg/issues/3576

Change 'echo' in the code to 'return':

/**
     * Add Display Job Categories Shortcode: [list_categories]
     *
     * https://wpjobmanager.com/customization-snippets/#displayCat
     */
    public function dm_display_wpjm_categories () {
        $terms = get_terms( array(
            'taxonomy' => 'job_listing_category',
            'hide_empty' => false,
        ) );

        $output = '';

        if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
//          echo '<ul>';
            $output .= '<ul>';
            foreach ( $terms as $term ) {
//              echo '<li>' . '<a href="' . esc_url( get_term_link( $term ) ) . '">' . $term->name . '</a></li>';
                $output .= '<li>' . '<a href="' . esc_url( get_term_link( $term ) ) . '">' . $term->name . '</a></li>';
            }
//          echo '</ul>';
            $output .= '</ul>';
        }

        return $output;
    }
tripflex commented 5 years ago

Yes this is correct, from this snippet: https://gist.github.com/davoraltman/98920d5d06aa717cfce2f9559108cb52#file-functions-php