PHP - function in Wordpress plugin itself causing error 500

Asked

Viewed 45 times

1

I developed a plugin for inserting shortcodes in Wordpress. It was my first, for personal use, has been working for years.

But a few weeks ago, it started to cause error 500 (POST wp-admin/admin-ajax.php 500) when trying to insert images in the article editor. I can upload the file, but by clicking Add media and choose some, nothing - and gives the error in the console:

inserir a descrição da imagem aqui

Activating debug.log, I have this:

PHP Fatal error: Uncaught Argumentcounterror: Too few Arguments to Function give_linked_media(), 3 passed in /wp-includes/class-wp-hook.php on line 286 and at least 7 expected in wp-content/plugins/tt-shortcodes/tt-functions.php:205

This is the function of line 205:

function give_linked_media( $html, $id, $caption, $title, $align, $url, $size, $alt = '' ) {
    // only if user is sending video or image
    if ( ( wp_attachment_is( 'video', $id ) ) || ( wp_attachment_is_image( $id ) ) ) {

        // build datatype var   
        if ( wp_attachment_is( 'video', $id ) ) {
            $datatype = ' data-lightview-type="iframe" ';
        } else if ( wp_attachment_is_image( $id ) ) {
            $datatype = ' data-lightview-type="image" ';
        } else { }

        // setting lightview params
        if ( empty( get_post_meta( $id, "pop_title", true ) ) ) {
            $poptitle = '';
        } else {
            $poptitle = ' data-lightview-title="' . get_post_meta( $id, "pop_title", true ) . '"';
        }
        if ( empty( get_post_meta( $id, "pop_caption", true ) ) ) {
            $popcaption = '';
        } else {
            $popcaption = ' data-lightview-caption="' . get_post_meta( $id, "pop_caption", true ) . '"';
        }
        if ( empty( get_post_meta( $id, "pop_group", true ) ) ) {
            $popgroup = '';
        } else {
            $popgroup = ' data-lightview-group="' . get_post_meta( $id, "pop_group", true ) . '"';
        }

        // if user decided to don't use, don't replace original html
        $tha_meta = get_post_meta( $id, 'lightbox_on', true );

        // regex bad, DOM good
        // http://stackoverflow.com/a/3820783/2234159
        $dom = new DOMDocument;
        $dom->loadHTML( $html );
        // get the link rel
        foreach ( $dom->getElementsByTagName( 'a' ) as $node ) {
            $therel = $node->getAttribute( 'rel' );

            // get the attachment val, if exists
            $therel = explode( ' ', $therel, 2 );
            $therel = $therel[ 0 ];
        }

        if ( $tha_meta == '1' ) { // is checkbox was set
            if ( $therel == 'attachment' ) { // if yes, no candy for you
                $html2 = $html;
            } else { // no attachment? Oh yeah
                $html2 = preg_replace( '/(<a.*?)>/', '$1 class="lightview"' . $poptitle . $popcaption . $popgroup . $datatype . 'fufu="' . $therel . '">', $html );
            }
        } else { // no checkbox? No candy
            $html2 = $html;
        }
    } else {
        $html2 = $html;
    }

    return $html2;
}
add_filter( 'media_send_to_editor', 'give_linked_media', 10, 8 );
  • Have you made any modifications to the Wordpress source code?

  • None. But from what I’m reading elsewhere, it can be caused by upgrading PHP from 5.6 to 7.*; I used Easyapache for this a few days ago, in fact. https://stackoverflow.com/questions/48099437/error-only-with-php-7-1-how-to-solve-the-too-few-arguments-to-function-get-quer

  • 1

    Tip: Your https://memoriabit.com.br/wp-includes/ directory and https://memoriabit.com.br/wp-content/uploads/ directory are open and may pose a risk to your site, create a file. htaccess and put the following code <Directory "/var/www/html/test">Options -Indexes</Directory>

  • Good, done. And I ended up discovering the answer to the question...

  • Consider commenting on your problem solution with an answer, this may help other users:D

  • Yeah, I’m just making sure with some tests here and I’ll update.

Show 1 more comment

1 answer

1

Just give a value to each var at the beginning of the function. Instead of:

function give_linked_media( $html, $id, $caption, $title, $align, $url, $size, $alt = '' )

Became:

function give_linked_media( $html = '', $id = '', $caption = '', $title = '', $align ='', $url = '', $size = '', $alt = '' )

Solved.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.