Help fix - code not compatible with older version of PHP

I have a theme that breaks because the code doesn’t play nice with my shared hosting’s PHP.

My browser says line 3 (PARSE ERROR, SYNTAX ERROR) is the problem. I tried a few online php validators, the newer versions okayed this code, but not the older versions.

Can someone help? I don’t know much of php.

[code]<?php
global $attachmentsName;
$attachments = new Attachments( $attachmentsName ?: ‘customposttypes_attachments’);?>

<?php if( $attachments->exist() ){ $images = array(); while( $attachments->get() ) { $images[] = array( 'id' => $attachments->id() , 'url' => $attachments->src( 'full' ) , 'thumbnail' => $attachments->src( 'thumbnail' ) ); } ?>[/code]

shouldn’t there be a value of some sort after the ternary operator??? I think this is why you are getting an error.

Change this line:

[php]$attachments = new Attachments( $attachmentsName ?: ‘customposttypes_attachments’);?>[/php]

to
[php]$attachments = new Attachments( $attachmentsName ? ‘’ : ‘customposttypes_attachments’);?>[/php]

Oh my!

Thank you so much tanzeelniazi!!! That removes the error and the page loads now.

BUT the gallery that is supposed to be there is not there. Maybe something else is the reason.

I really, really appreciate you help. I don’t have much, but I am sending $10 paypal for your help.

Thank you very much for your donation.

Well it’s hard to tell from this piece of code. Do a var_dump on $attachmentsName.

add:
[php] var_dump($attachmentsName); [/php]

after:
[php]global $attachmentsName;[/php]

run the script and tell me what do you get and if possible share Attachments class too.

You’re welcome. And sorry, it’s not much.

  1. I get this on my browser

string(11) “attachments”

  1. Where do I find the class? I tried looking for attachments in the theme’s function.php. I see this.

[code]function lmx_attachments( $attachments )
{
$fields = array(
array(
‘name’ => ‘title’, // unique field name
‘type’ => ‘text’, // registered field type
‘label’ => __( ‘Title’, ‘attachments’ ), // label to display
‘default’ => ‘title’, // default value upon selection
)
);

$args = array(
‘label’ => ‘Attachments Gallery’,
‘post_type’ => array( ‘accommodations’, ‘dining’ ),
‘position’ => ‘normal’,
‘priority’ => ‘default’,
‘filetype’ => array(‘image’), // no filetype limit
‘append’ => true,
‘button_text’ => __( ‘Attach Files’, ‘attachments’ ),
‘modal_text’ => __( ‘Attach’, ‘attachments’ ),

// which tab should be the default in the modal (string) (browse|upload)
'router'        => 'browse',
'fields'        => $fields,

);
$attachments->register( ‘customposttypes_attachments’, $args );
}

add_action( ‘attachments_register’, ‘lmx_attachments’ );[/code]

Never mind about the Attachments class, we’ll look into it later. Tell me what results do you get by doing var_dump write down all the values.

I get

string(11) "attachments"

That’s all.

It should actually display a gallery of images.

ok. then change

[php]$attachments = new Attachments( $attachmentsName ? ‘’ : ‘customposttypes_attachments’);?>[/php]

to:
[php]$attachments = new Attachments( $attachmentsName ? ‘attachments’ : ‘customposttypes_attachments’);?>[/php]

or
[php]$attachments = new Attachments( $attachmentsName ? $attachmentsName : ‘customposttypes_attachments’);?>[/php]

and tell what do you get??

Awesome. Both codes give me

string(11) "attachments"

and the gallery!

Thank you so much for your help tanzeelniazi. I guess time to delete the var dump output and start working on the site. Again, thank you for your help.

You are welcome.

That’s good, var_dump can be a good option to debug your code and to see what kind of value you are getting in variables.

Yeah you can delete the var_dump line. It’s better to use.
[php]$attachments = new Attachments( $attachmentsName ? $attachmentsName : ‘customposttypes_attachments’);?>[/php]

Okay. Will use that.

Thank you for your guidance and help. They are simple, straighforward and very helpful.

Very glad that I met you first here. ;D

Sponsor our Newsletter | Privacy Policy | Terms of Service