farinspace / wpalchemy

Thin framework for wordpress
http://wpalchemy.com/
Other
416 stars 110 forks source link

_global_head is being called 3 times - breaks have_fields_and_multi #111

Open delicado54 opened 8 years ago

delicado54 commented 8 years ago

Using WP 4.6.1 and wpalchemy 1.6.1, I find that using have_fields_and_multi produces 3 new groups every time I click add (that is, $('[class*=docopy-]').click(function(e) on line 1354 seems to be being triggered 3 times even though I have only one item with a class of docopy-.... I assumed this was my error but I found it also happens with the out-of-the-box full-meta.php.

If I revert to 1.5.x or earlier, it works again, but I get a bunch of deprecation notices.

After a bit of digging around I realised that the javascript block is being printed out on the page 3 times - so it looks like the function _global_head() is being triggered 3 times.

It looks like I have a temporary fix by inserting e.stopImmediatePropagation(); on line 1356 after e.preventDefault(). But this might cause other issues and obviously isn't a real solution.

delicado54 commented 8 years ago

As suggested in the comment on line 460 ("// todo: when first run define a constant to prevent other instances from running again ..."), I was able to prevent this 3-times outputting with the following code:

if(!defined('HEAD_CALLED')): add_action( 'admin_head', array( $this, '_global_head' ) ); define('HEAD_CALLED', true); endif;

stratboy commented 8 years ago

Thank you very much @delicado54! I had the same issue and was able to fix swapping line 460 with yours. Should be a pull request.

masterbip commented 6 years ago

You have just saved my life. 5 days and this is the ONLY reference we got related to this repeating fields issue. Thanks!!!

BTW: This must be added to MetaBox.php original file

stratboy commented 6 years ago

BTW: I finally started using Advanced Custom Fields plugin. After several years of wpalchemy. I'll never go back. Since wpalchemy isn't maintained, I strongly suggest the switch to everyone. This is not a spot, just developer to developer help..

delicado54 commented 6 years ago

Glad to have helped, masterbip!

I also switched to Advanced custom fields over the past year. A bit of a departure to have the forms etc defined via a GUI and stored in the database, but it's a very powerful plugin!

masterbip commented 6 years ago

I try to avoid using plugins as much as possible, but I'll try that someday. Many thanks for the suggestion!

stratboy commented 6 years ago

Same here, but in this case, really, saying it's really well done is not enough...

And keep in mind that wpalchemy IS a plugin, indeed. And that you're now loosing time for the very same reason you probably fear plugins: dependency and reliability. So... :)

farinspace commented 6 years ago

With any piece of software comes maintenance, and as many have noticed, wpalchemy has gone unmaintained for a long time now (my apologies).

I think plugins are great, in most of my day-to-day dev, we use plugins .. publicly available (vetted and curated) and our own private ones. Again the idea here is maintenance and repeatable success.

While I enjoy solving problems, it is not fun to have to touch code for mundane tasks, plugins with decent UI's help solve this. So, I too encourage the use of plugins :-)

delicado54 commented 6 years ago

Dimas - many thanks for wp alchemy and all your work on it!

rebecaescandon commented 3 years ago

@delicado54 I added a Pull request with your suggestion, it's working on our project , thank you! https://github.com/farinspace/wpalchemy/pull/120 @farinspace Could you please take a look? Thank you.