0

I have the script below and I need to return both of the conditional return values -- each result on a line. Is there any way to format it as an array in order to work ?

The script is:

if ($pdf_url != '') {
    if ($title != '') {
        $title_from_url = $this->make_title_from_url($pdf_url);
        if ($title == $title_from_url || $this->make_title_from_url('/'.$title) == $title_from_url) {
            // This would be the default title anyway based on URL
            // OR if you take .pdf off title it would match, so that's close enough - don't load up shortcode with title param
            $title = '';
        } else {
            $title = ' title="' . esc_attr( $title ) . '"';
        }
    }
    return apply_filters('pdfemb_override_send_to_editor', '[pdf-embedder url="' . $pdf_url . '"'.$title.']', $html, $id, $attachment);
} else {
    return $html;
}
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Marius S
  • 1
  • 2

1 Answers1

0

If you want each result on a line, it sounds like you could use a temporary variable, and concatenate to it based on the logic.

 <?php
      $output = '';
      if ($pdf_url != '') {
           if ($title != '') {
                $title_from_url = $this->make_title_from_url($pdf_url);
                if ($title == $title_from_url || $this->make_title_from_url('/'.$title) == $title_from_url) {
                     // This would be the default title anyway based on URL
                     // OR if you take .pdf off title it would match, so that's close enough - don't load up shortcode with title param
                     $title = '';
                } else {
                     $title = ' title="' . esc_attr( $title ) . '"';
                }
           }
           // You may need print_r() around apply_filters() if it returns an array
           $output .= apply_filters('pdfemb_override_send_to_editor', '[pdf-embedder url="' . $pdf_url . '"'.$title.']', $html, $id, $attachment) . "\n";
      } else {
           $output .= $html . "\n";
      }
      return $output;

Or if you want it in array:

 <?php
      $output = [];
      if ($pdf_url != '') {
           if ($title != '') {
                $title_from_url = $this->make_title_from_url($pdf_url);
                if ($title == $title_from_url || $this->make_title_from_url('/'.$title) == $title_from_url) {
                     // This would be the default title anyway based on URL
                     // OR if you take .pdf off title it would match, so that's close enough - don't load up shortcode with title param
                     $title = '';
                } else {
                     $title = ' title="' . esc_attr( $title ) . '"';
                }
           }
           // You may need print_r() around apply_filters() if it returns an array
           $output[] = apply_filters('pdfemb_override_send_to_editor', '[pdf-embedder url="' . $pdf_url . '"'.$title.']', $html, $id, $attachment);
      } else {
           $output[] = $html;
      }
      return $output;
mikey
  • 1,068
  • 10
  • 13
  • 1
    hello,thanks for so quick answer. Both ways are giving me same error in website: There has been a critical error on this website. Please check your site admin email inbox for instructions.I will check logs to see if i can find more. – Marius S Dec 15 '20 at 14:52
  • This is the error in debug.log: PHP Parse error: syntax error, unexpected 'else' (T_ELSE), expecting function (T_FUNCTION) or const (T_CONST) on that last else of the code (penultimate line of original code) – Marius S Dec 15 '20 at 15:00
  • Can you see a line number referenced anywhere? I ran the code I sent you locally and I don't get that error. – mikey Dec 15 '20 at 15:05
  • 1
    @MariusS are you sure that you have properly opened/closed all curly braces? – Akhilesh B Chandran Dec 15 '20 at 15:13
  • full code here http://qqqq.ro/code.txt whith this code now error is [15-Dec-2020 15:17:56 UTC] PHP Parse error: syntax error, unexpected 'protected' (T_PROTECTED), expecting end of file in core_pdf_embedder.php on line 109 – Marius S Dec 15 '20 at 15:23
  • Thanks @MariusS, I've had a look, it seems when you pasted in the code from here, there was an extra curly brace added on line 108. So after `return $output;` there should only be 1x `}`. – mikey Dec 16 '20 at 00:59
  • Thanks @mikey . Now i don't have any error but as result i still have only the 1'st result (of this line return apply_filters('pdfemb_override_send_to_editor', '[pdf-embedder url="' . $pdf_url . '"'.$title.']', $html, $id, $attachment);) i also want to have the result of return $html; on the next line (now it is an else statement. I need both result). Thank you very much. All your help is appreciated. – Marius S Dec 16 '20 at 07:20
  • No worries @MariusS, so the code you send us in that URL is using my 2nd example, perhaps try the first code block I sent instead. Without knowing where/how the output of that function is used it makes it hard to be certain. – mikey Dec 17 '20 at 01:16