Last active
April 4, 2023 12:24
-
-
Save rloes/69d8d56437dc0793b153f096fd88b1ba to your computer and use it in GitHub Desktop.
WordPress: Add Code Snippets to Elementor Finder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use function Code_Snippets\code_snippets; | |
function add_new_finder_items( array $categories ) { | |
$categories['settings']['items']['elementor-snippets'] = [ | |
'title' => esc_html__( 'Elementor Custom Code', 'textdomain' ), | |
'icon' => 'elementor', | |
'url' => site_url().'/wp-admin/edit.php?post_type=elementor_snippet', | |
'keywords' => [ 'custom', 'code'], | |
]; | |
$categories['settings']['items']['all-code-snippet'] = [ | |
'title' => esc_html__( 'Alle Code Snippets', 'textdomain' ), | |
'icon' => 'code-bold', | |
'url' => site_url().'/wp-admin/admin.php?page=snippets', | |
'keywords' => [ 'code', 'snippets', 'all', 'alle'], | |
]; | |
$categories['create']['items']['add-code-snippet'] = [ | |
'title' => esc_html__( 'Code Snippet hinzufügen', 'textdomain' ), | |
'icon' => 'plus-circle-o', | |
'url' => site_url().'/wp-admin/admin.php?page=add-snippet', | |
'keywords' => [ 'code', 'snippet', 'add', 'hinzufügen'], | |
]; | |
return $categories; | |
} | |
add_filter( 'elementor/finder/categories', 'add_new_finder_items' ); | |
// create Snippet class | |
class Code_Snippets_Category extends \Elementor\Core\Common\Modules\Finder\Base_Category { | |
public function get_id() { | |
return 'code-snippets'; | |
} | |
public function get_title() { | |
return esc_html__( 'Code Snippets', 'textdomain' ); | |
} | |
public function get_category_items( array $options = [] ) { | |
$snippets = get_snippets(); | |
foreach($snippets as $snippet){ | |
$options[sanitize_title($snippet->name.$snippet->id)] = [ | |
'title' => esc_html__( 'Snippet bearbeiten: '.$snippet->name, 'textdomain' ), | |
'icon' => 'code-bold', | |
'url' => code_snippets()->get_snippet_edit_url($snippet->id), | |
'keywords' => [ 'snippet', 'code', $snippet->name], | |
]; | |
} | |
return $options; | |
} | |
} | |
function register_new_finder_category( $finder_categories_manager ) { | |
$finder_categories_manager->register( new Code_Snippets_Category() ); | |
}; | |
add_action( 'elementor/finder/register', 'register_new_finder_category' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment