wp_send_json_error example

if ( ! is_user_logged_in() ) {
            wp_die( 0 );
        }

        check_ajax_referer( 'update-widget', 'nonce' );

        if ( ! current_user_can( 'edit_theme_options' ) ) {
            wp_die( -1 );
        }

        if ( empty( $_POST['widget-id'] ) ) {
            wp_send_json_error( 'missing_widget-id' );
        }

        /** This action is documented in wp-admin/includes/ajax-actions.php */
        do_action( 'load-widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
        /** This action is documented in wp-admin/includes/ajax-actions.php */
        do_action( 'widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
        /** This action is documented in wp-admin/widgets.php */
        do_action( 'sidebar_admin_setup' );

        
public function handle_render_partials_request() {
        if ( ! $this->is_render_partials_request() ) {
            return;
        }

        /* * Note that is_customize_preview() returning true will entail that the * user passed the 'customize' capability check and the nonce check, since * WP_Customize_Manager::setup_theme() is where the previewing flag is set. */
        if ( ! is_customize_preview() ) {
            wp_send_json_error( 'expected_customize_preview', 403 );
        } elseif ( ! isset( $_POST['partials'] ) ) {
            wp_send_json_error( 'missing_partials', 400 );
        }

        // Ensure that doing selective refresh on 404 template doesn't result in fallback rendering behavior (full refreshes).         status_header( 200 );

        $partials = json_decode( wp_unslash( $_POST['partials'] ), true );

        if ( ! is_array( $partials ) ) {
            wp_send_json_error( 'malformed_partials' );
        }


/** * Generate the personal data export file. * * @since 4.9.6 * * @param int $request_id The export request ID. */
function wp_privacy_generate_personal_data_export_file( $request_id ) {
    if ( ! class_exists( 'ZipArchive' ) ) {
        wp_send_json_error( __( 'Unable to generate personal data export file. ZipArchive not available.' ) );
    }

    // Get the request.     $request = wp_get_user_request( $request_id );

    if ( ! $request || 'export_personal_data' !== $request->action_name ) {
        wp_send_json_error( __( 'Invalid request ID when generating personal data export file.' ) );
    }

    $email_address = $request->email;

    
$all_items  = array();
        $item_types = array();
        if ( isset( $_POST['item_types'] ) && is_array( $_POST['item_types'] ) ) {
            $item_types = wp_unslash( $_POST['item_types'] );
        } elseif ( isset( $_POST['type'] ) && isset( $_POST['object'] ) ) { // Back compat.             $item_types[] = array(
                'type'   => wp_unslash( $_POST['type'] ),
                'object' => wp_unslash( $_POST['object'] ),
                'page'   => empty( $_POST['page'] ) ? 0 : absint( $_POST['page'] ),
            );
        } else {
            wp_send_json_error( 'nav_menus_missing_type_or_object_parameter' );
        }

        foreach ( $item_types as $item_type ) {
            if ( empty( $item_type['type'] ) || empty( $item_type['object'] ) ) {
                wp_send_json_error( 'nav_menus_missing_type_or_object_parameter' );
            }
            $type   = sanitize_key( $item_type['type'] );
            $object = sanitize_key( $item_type['object'] );
            $page   = empty( $item_type['page'] ) ? 0 : absint( $item_type['page'] );
            $items  = $this->load_available_items_query( $type$object$page );
            if ( is_wp_error( $items ) ) {
                
check_ajax_referer( 'community_events' );

    $search         = isset( $_POST['location'] ) ? wp_unslash( $_POST['location'] ) : '';
    $timezone       = isset( $_POST['timezone'] ) ? wp_unslash( $_POST['timezone'] ) : '';
    $user_id        = get_current_user_id();
    $saved_location = get_user_option( 'community-events-location', $user_id );
    $events_client  = new WP_Community_Events( $user_id$saved_location );
    $events         = $events_client->get_events( $search$timezone );
    $ip_changed     = false;

    if ( is_wp_error( $events ) ) {
        wp_send_json_error(
            array(
                'error' => $events->get_error_message(),
            )
        );
    } else {
        if ( empty( $saved_location['ip'] ) && ! empty( $events['location']['ip'] ) ) {
            $ip_changed = true;
        } elseif ( isset( $saved_location['ip'] ) && ! empty( $events['location']['ip'] ) && $saved_location['ip'] !== $events['location']['ip'] ) {
            $ip_changed = true;
        }

        

    }

    /** * Handles customize_save WP Ajax request to save/update a changeset. * * @since 3.4.0 * @since 4.7.0 The semantics of this method have changed to update a changeset, optionally to also change the status and other attributes. */
    public function save() {
        if ( ! is_user_logged_in() ) {
            wp_send_json_error( 'unauthenticated' );
        }

        if ( ! $this->is_preview() ) {
            wp_send_json_error( 'not_preview' );
        }

        $action = 'save-customize_' . $this->get_stylesheet();
        if ( ! check_ajax_referer( $action, 'nonce', false ) ) {
            wp_send_json_error( 'invalid_nonce' );
        }

        

function wp_ajax_press_this_save_post() {
    _deprecated_function( __FUNCTION__, '4.9.0' );
    if ( is_plugin_active( 'press-this/press-this-plugin.php' ) ) {
        include WP_PLUGIN_DIR . '/press-this/class-wp-press-this-plugin.php';
        $wp_press_this = new WP_Press_This_Plugin();
        $wp_press_this->save_post();
    } else {
        wp_send_json_error( array( 'errorMessage' => __( 'The Press This plugin is required.' ) ) );
    }
}

/** * Ajax handler for creating new category from Press This. * * @since 4.2.0 * @deprecated 4.9.0 */
function wp_ajax_press_this_add_category() {
    _deprecated_function( __FUNCTION__, '4.9.0' );
    

    public function ajax_background_add() {
        check_ajax_referer( 'background-add', 'nonce' );

        if ( ! current_user_can( 'edit_theme_options' ) ) {
            wp_send_json_error();
        }

        $attachment_id = absint( $_POST['attachment_id'] );
        if ( $attachment_id < 1 ) {
            wp_send_json_error();
        }

        update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_stylesheet() );

        wp_send_json_success();
    }

    
/** * Gets attachment uploaded by Media Manager, crops it, then saves it as a * new object. Returns JSON-encoded object details. * * @since 3.9.0 */
    public function ajax_header_crop() {
        check_ajax_referer( 'image_editor-' . $_POST['id'], 'nonce' );

        if ( ! current_user_can( 'edit_theme_options' ) ) {
            wp_send_json_error();
        }

        if ( ! current_theme_supports( 'custom-header', 'uploads' ) ) {
            wp_send_json_error();
        }

        $crop_details = $_POST['cropDetails'];

        $dimensions = $this->get_header_dimensions(
            array(
                'height' => $crop_details['height'],
                

        if ( isset( $_GET['_wp-find-template'] ) ) {
            wp_send_json_success( $block_template );
        }
    } else {
        if ( $template ) {
            return $template;
        }

        if ( 'index' === $type ) {
            if ( isset( $_GET['_wp-find-template'] ) ) {
                wp_send_json_error( array( 'message' => __( 'No matching template found.' ) ) );
            }
        } else {
            return ''; // So that the template loader keeps looking for templates.         }
    }

    // Add hooks for template canvas.     // Add viewport meta tag.     add_action( 'wp_head', '_block_template_viewport_meta_tag', 0 );

    // Render title tag with content, regardless of whether theme has title-tag support.
Home | Imprint | This part of the site doesn't use cookies.