Trigger execution of all custom fields mapping rules through wp cli
-
Hello, David
Is it possible to trigger the mapping of custom fields using wp-cli? I notice that wp-cli has some options of running php code, and I was hoping to be able to run the mapping of custom fields through cli.
https://developer.www.ads-software.com/cli/commands/media/regenerate/
Im thinking that it might be possible to run a php script using –exec
-
Or, of course: trigger a php-function directly from the command line.
Maybe something like “mla_begin_mapping” or “mla_mapping_custom_value”?Tryng to learn (although this doesn’t work):
Could I trigger execution of mapping existing custom fields function by f.ex “php customfieldmapping.php” in wp-content dir?
<?PHP require_once(explode("wp-content", __FILE__)[0] . "wp-load.php"); do_action('mla_mapping_settings', 'rating', '5449', 'custom_field_mapping', ''); ?>
Thanks for an interesting question, which is a variation on this earlier topic:
“IPTC/EXIF Mapping Actions” during wp-cli “–skip-copy” import
There are two small example plugins that show you how to accomplish something similar to your goal:
- MLA Bulk Edit Remap Example – Performs IPTC/EXIF and Custom Field mapping at the conclusion of a Bulk Edit action, so data sources like “terms:” are properly applied.
- MLA Postie Post After Example – Applies MLA mapping rules to child attachments after Postie creates a parent post from an email.
You should be able to adapt the ideas there to your application. I’m not sure if the
MLAOptions
class is loaded forwp-cli
operations. If that’s a problem, let me know.I will leave this topic unresolved in case you have problems or further questions regarding the code in the example plugins. Thanks for your interest in MLA.
Thank you for your reply!
Yes, i did find that topic, but unfortunately I didn’t find the actual solution for the issue.
I have also looked over both the example plugins during these last days, and I’ve been pulling my hair out in frustration for not understanding how I am going to actually “use” them… I have edited php and various functions in WordPress for many years, when needed, but for some reason I always struggle with seeing the logic of it all. And in the end I’m just happy with getting something to work, without properly understanding “why” it worked (or admit defeat and try to put it aside and forget about it).
I guess my coding-gene is not properly activated ??
can you give me an example of a php script that trigger the “execute all rules” function found in the custom fields mapping?
If I have a php script that I can trigger from the cli with something like “php customfieldmapping.php” I could easily have it triggered by cron, a watchdog and similar
Thanks for looking over the example plugins and giving me a better sense of your PHP/WordPress comfort level.
I have not worked with wp-cli, but I will investigate further and see if I can come up with a suitable solution. I will post an update here when I have progress to report. Thanks for your patience and understanding.
Thank you, David. I’m looking forward to it! Im looking at both “wp media import” and “wp media regenerate” as possible candidates to also trigger MLA custom field mapping
PS: wp-cli is not an absolute requirement, it can be some other command line method also. I can trigger a php script through command line by executing “php <location of script>” too, and as far as I know its possible to make sure all wp functions and plugin functions are loaded and available for the php-script by setting require to location of wp-load.php.
@pivot is it a must that you use wp-cli for this?
@bernardberry646 nope. I can also execute a php script through regular command line (linux bash)
@bernardberry646 any suggestions?
@pivot sorry about the delay.. I know exactly how you feel, you are not going to let this beat you lol.
I asked if wp-cli was a must, meaning wp-cli and terminal.. but probably moreso “manually”. So, at the very basics, you want to “manually” do a custom field mapping. But you never said exactly what you had thus far code wise.
MLA has ALOT of hooks, so ‘triggering’ something wouldn’t be an issue! It’s the exact ‘what do you want to happen’ and ‘when’. You mention ‘terminal’ and ‘wp-cli’, so it appears that YOU want to manually execute this or do you want this script to run automatically at some point during MLA’s execution of some action?
I ask the above because:
1. automatic execution or you wanting to manually execute this script is important to know.
2. if not manually, as in no need for wp-cli / terminal commands.. then its much easier.. could just hook right on to one of the many mla hooks.waiting your response..
@bernardberry646 no worries ??
at the moment I have a “watchdog” monitoring /var/www/wp-contents/uploads directory (inotify-hookable). whenever changes happen (new file uploaded, changed metadata in a file, file deleted, etc) it will trigger a command.
right now the watchdog trigger 2 commands every time a change happens:
1. php /var/www/wp-content/plugins/bulk-media-register-add-on-wpcron/lib/bmrcroncli.php (a plugin I use to add new files to media library if they dont exists already)
2. wp media delete-missing (add-on for wp-cli that I use to delete any media in media library that doesn’t have a source file in /var/www/wp-content/uploads)with this I am able to always keep my wordpress media library in sync with a source folder on another host. I am able to sync metadata to various custom fields in wordpress by manually executing the rulles from MLA backend, but so far I have not been able to fully automate it.
so, in addition to having the watchdog trigger the execution of mapping rules there could be other ways too:
hooking on to the bulk-media-register-add-on-wpcron/lib/bmrcroncli.php script
or “simply” writing a new function/plugin that watch for some relevant hook (or action?) and then triggering the custom fields mapping. this could be a whole range of things, I’m sure, but it would possibly be something related to the internal processes of wordpress registering new media to the media library
I’m thinking that this might also introduce another benefit: not needing to execute custom field mapping for every single image, but instead trigger it for only the new media (or the ones missing)…right?
I have looked at various automation plugins for wordpress too but they are all way overkill for what I need at the moment, and figuring out how to use them could be a bigger task than just figuring out a code for a php script ??
-
This reply was modified 1 year, 9 months ago by
pivot.
Thanks, @bernardberry646, for drawing out the details of this topic’s request. I was thinking of a solution that would run the mapping rules for all Media Library items manually, but it looks like that’s not the best solution. As you observed, MLA has lots of hooks and a more targeted solution is possible.
@pivot – you suggested “hooking on to the bulk-media-register-add-on-wpcron/lib/bmrcroncli.php script“. Can you tell me more about this plugin? Is it in the Repository or somewhere I can get a copy?
There have been several earlier topics about different ways to upload or register items in different ways. If they follow the WordPress process to add the item MLA should run its mapping rules. Sometimes, though, MLA’s code isn’t loaded because the upload happens with an AJAX request. If that’s the case here the solution is simple. Have a look at this earlier topic:
IPTC/EXIF Mapping won’t auto execute upon upload | www.ads-software.com
You can find more information about the “MLA_AJAX_EXCEPTIONS” option in the “IPTC & EXIF Processing Options” section of the Settings/Media Library Assistant Documentation tab. If that doesn’t work for you, any additional details you can provide will help me investigate further. For example, if you just want to run mapping for one or a few new items and you know their IDs, I can adjust my solution.
@dglingren hanks for following up on this!
Its an add-on for the plugin I mention here: https://www.ads-software.com/support/topic/unable-to-map-attachment-file-metadata-such-as-pngdream/#post-16769114
its a premium plugin with a one time fee, and it makes the Bulk Media Register plugin capable of being triggered by cron, command line, watchdog or similar bash execution.
But, unfortunately the MLA_AJAX_EXCEPTIONS didn’t work for the premium add-on, only for the base plugin, so when its triggered by the bash command “php /var/www/wp-content/plugins/bulk-media-register-add-on-wpcron/lib/bmrcroncli.php” it doesn’t trigger the same functions as the base plugin..
I can send you the plugin by e-mail and yo can have a look?
For example, if you just want to run mapping for one or a few new items and you know their IDs, I can adjust my solution.
Yes, that would be optimal. But, at the moment I am not sure how to fetch this, because the command “php /var/www/wp-content/plugins/bulk-media-register-add-on-wpcron/lib/bmrcroncli.php” runs silently. If you find anything in the code for it that I can use then that’s great, and I could also contact the plugin author to ask (but from the plugin settings I cannot see any simple way of fetching this when running the command)
Thanks for your additional information. As I did some research I came across this earlier MLA support topic:
Compatibility with “Bulk Media Register”-plugin or trigger mapping manually | www.ads-software.com
I regret I didn’t find it earlier, but it gave me the clues I needed to attempt a fix for your application. I have added some code to the
MLACore::initialize()
function to detect the plugin you’re using and add MLA’s mapping support to the registration action. @leanderbraunschweig – this fix might work for you, too.I have uploaded a new MLA Development Version dated 20230610 that contains the fix. You can find step-by-step instructions for using the Development Version in this earlier topic:
How to download & install the current development version of MLA
Once the Development Version is installed you can retest the handling of mapping rules during the Bulk Media Register cron job processing.
The fix will be part of my next MLA version, but in the interim it would be great if you could install the Development Version and let me know if it works for you. Thanks for alerting me to this issue.
If the problem still occurs, you can contact me at my web site to send any additional materials that will help me investigate further. Thanks for your help and your patience.
- The topic ‘Trigger execution of all custom fields mapping rules through wp cli’ is closed to new replies.