2

I am using the following filter to run a function that generates a CSS file after a plugin gets updated. Currently the CSS gets generated when any plugin gets updated. Is there a simple way to limit this running to a particular plugin?

add_filter('upgrader_post_install', 'generate_my_css', 100, 0);

function generate_my_css() {
    $ss_dir = get_stylesheet_directory();
    $pi_dir = plugin_dir_path( __FILE__ );
    ob_start();
    require($pi_dir . 'includes/css/mycss.php');
    $css = ob_get_clean();
    file_put_contents($ss_dir . '/css/mycss.css', $css, LOCK_EX);
}

Ideally the above code would live in the plugin that was being updated but could just as easily live in the functions.php

2
  • 1
    Important things to show in your question when you edit it: 1. Where this code is being initialized. 2. How you have NAMESPACED the very simply named restore function. 3. The code that is in the function restore; Jan 20, 2016 at 4:37
  • Hopefully the above will explain a bit better.
    – PthPndr
    Jan 20, 2016 at 12:19

2 Answers 2

1

upgrader_post_install has three parameters $response, $hook_extra and $result which give you extra information. At the moment I can't take a look myself, but I'm assuming that especially the $result variable should give you additional information to differentiate.

1
  • The $result actually contained a lot of information but the $hook_extra just contained the plugin_basename which worked out perfectly.
    – PthPndr
    Jan 21, 2016 at 12:14
0
add_filter('upgrader_post_install','upgrader_post_install_func',10,3);

function upgrader_post_install_func($response,$hook_extra,$result){
      /*
          visualizar $result y $hook_extra, se guarda en carpeta wp-content.
       */
      
      #file_put_contents(WP_CONTENT_DIR . '/test.txt', serialize($result)."\r\n", FILE_APPEND);

    /* renombrar complemento actualizado desde github zipball, para mantener 
       actualizaciones dado que github al descargar zip y descomprimirlo lo pone en 
       este formato: <usuario github>-<nombre repo>-<codigo version>
       y se necesita <nombre repo>-<main | master>
    */
    $plugin = explode('/', $hook_extra['plugin']); 
    rename(WP_PLUGIN_DIR .'/'. $result['destination_name'],WP_PLUGIN_DIR .'/'.$plugin[0]);
    return $result;
}

Espero ser de ayuda.

1
  • 1
    Please edit your answer, and add an explanation: why could that solve the problem?
    – fuxia
    Dec 22, 2021 at 8:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.