Support » Developing with WordPress » add_filter // reuse argument

  • Hi,
    I’m a newbie in the world of hooks & WordPress & Php …
    I’m trying to reuse a Argument ($shape_data) I’ve previoulsy get from the Filter: save_my_shape.

    add_filter('save_my_shape', 'save_me', 10, 2);
    function save_me (  $shapes_html, $shapes_data ) {	
    return $shapes_html;

    I’m not sure how to reuse $shapes_data wherever than save_me().

    I’ve tried to add another function my_get_shapes_datas() into save_me() but it doesn’t work:

    add_filter('save_my_shape', 'save_me', 10, 2);
    function save_me (  $shapes_html, $shapes_data ) {
      function my_get_shapes_datas($shapes_data){
    				$result = $shapes_data;
    			return $result;
    		}
    	
    return $shapes_html;

    I think this is a commun need but I’ve no idea how to do it.

    Could you help?

    thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • Is this ‘save_my_shape’ filter from a plugin? It doesn’t sound like anything that WordPress has.

    WordPress hooks come in two flavors: filters and actions. Filters are just like the name says: the data passed in is changed (or not) and passed back, just like in a water pipe, the filter can affect the water passing through. Actions are sort of like alarm clocks. The function is called when it’s time to do that action. Those functions can receive data, but they often don’t. It’s useful for running code once its conditions are met (like don’t register widgets until the WP widget globals are setup).

    Sometimes, if the code doesn’t have any action hooks, you can make do with a filter hook. It’s not good to have side effects, but it might be the only way to get the data.

    The filter you are trying to use sounds like it is for modifying the data before it is saved. And it being saved implies that you could retrieve it again, so I’m not sure that you actually need to use this filter. But you should ask at the plugin’s support forum.

    Hi,
    It is a hook filter from a plugin, the only one that the author gave me.
    In this one, nativly made to modify the $shapes_html, he told me that I can retrieve the data I need by using the second arg: $shapes_data

    That’s why I’m using a hook type filter…

    My plan is to extract $shapes_data in another function. That’s why I tried to create another function into the first one to return $shapes_data only. A function that I could call wherever. But it doesn’t work.

    I could ask my author pluggin, and he probably will send me a code that does the job… but I would like to understand… how to pass theses datas.

    The filter function is only called at certain times, like when the data is being saved. You can’t define a function inside there, but you can call a function from the filter. But the data will not be available in the filter function until the plugin calls it (during save). It sounds like you need to save the shapes_data separately, so you can refer to it from other functions. You can do that in your filter function; you just have to decide where to save it.
    You can read https://codex.wordpress.org/Plugin_API/Hooks to learn more about hooks.

    Ok, so you’ve confirmed me that my method (to put a function into a function called by a add_filter) doesn’t work.
    thanks for that.

    I’ve read many things about hook, but I don’t have any other choice than to use add_filter since this is the only one available by this plugin.

    Yes I need to save the shape_data separately but how?
    by creating a class? by using global variables?

    I’m looking for best practices in my case.

    Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    Side note: @zunkytro Please take this seriously. If you report another topic again your account will be banned from this site.

    Your account is on moderation watch for cause due to your behavior. Check your email, that was explained to you already. You have now gone and reported over 10 topics and each one had to be looked at. There was no reason for that, the topics had nothing to do with you or your topics.

    If you you repeat that abuse, your account will be banned.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘add_filter // reuse argument’ is closed to new replies.