مثالهای بیشتر برای هوکهای activation و deactivation
ایجاد شده در ۱۷ اردیبهشت ۱۴۰۱
سلام. میشه چندتا مثال کاربردی و عینی برای این دوتا هوک بزنید؟ هوکهای activation و deactivation
سلام دوست عزیز، در فصلهای آتی خود استاد در پروژه هاشون از این دو استفاده خواهند کرد.
ولی یه نمونه از مثال هاش رو در زیر میارم:
مثلا برای تازه سازی پرمالینک وردپرس هنگامی است که به وسیله ی یک پلاگین میخوایم یک نوع پست سفارشی را ثبت کنیم:
و این قسمت برای activation هست:
functionpluginprefix_setup_post_type() {
// register the "book" custom post type
register_post_type( 'book', ['public' => 'true'] );
}
add_action( 'init', 'pluginprefix_setup_post_type' );
functionpluginprefix_install() {
// trigger our function that registers the custom post type
pluginprefix_setup_post_type();
// clear the permalinks after the post type has been registered
flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'pluginprefix_install' );
و این قسمت برای deactivation هست:
functionpluginprefix_deactivation() {
// unregister the post type, so the rules are no longer in memory
unregister_post_type( 'book' );
// clear the permalinks to remove our post type's rules from the database
flush_rewrite_rules();
}
register_deactivation_hook( __FILE__, 'pluginprefix_deactivation' );
به طور خلاصه بخوام بگم اینه که ما گفتیم زمانی که افزنه فعال شد پست تایپ book رو به وسیله هوک activation ،فعال کنه و زمانی که پلاگین غیر فعال میشه قائدتا باید اون پست تایپ حذف بشه و ما این کار رو توسط هوک deactivation انجام میدیم.