目錄
自訂功能
在我們的自訂貼文類型上註冊自訂功能
建立新的 WordPress 使用者角色,為其指派新的自訂功能
建立/編輯 WordPress 用戶,將他們指派給新的聯絡人角色
为其他角色分配自定义功能
清洁提升我们的角色
摘要
首頁 後端開發 php教程 在 WordPress 中建立基本 CRM:利用自訂功能

在 WordPress 中建立基本 CRM:利用自訂功能

Aug 29, 2023 pm 08:01 PM

我們一直在研究如何在 WordPress 中建立一個簡單的 CRM 系統。在本系列的最後一部分中,我們探討如何使用角色來限制使用者對 WordPress 管理介面部分內容的存取。

今天我們將介紹如何使用自訂功能僅限制對聯絡人自訂貼文類型的存取。

自訂功能

雖然我們之前降低了 WordPress 用戶的訪問級別,但透過將他們指派給編輯或作者角色,用戶仍然可以管理貼文和評論。這些功能對我們來說都沒有用,因此我們需要進一步限制訪問,允許指定的用戶只能訪問聯絡人自訂貼文類型。

我們可以透過以下方式做到這一點:

  1. 在我們的自訂貼文類型上註冊自訂功能
  2. 建立新的 WordPress 使用者角色,僅將我們新的自訂功能指派給該角色
  3. 建立/編輯 WordPress 用戶,並將其指派給新的聯絡人角色

在我們的自訂貼文類型上註冊自訂功能

註冊自訂功能可讓我們稍後定義哪些 WordPress 角色可以存取這些功能,例如,作者是否可以建立新的聯絡人。

讓我們編輯外掛程式檔案的 register_post_type() 函數調用,將 capability_type => 'post' 替換為以下內容:

'capabilities' => array(
    'edit_others_posts'		=> 'edit_others_contacts',
	'delete_others_posts'	=> 'delete_others_contacts',
	'delete_private_posts'	=> 'delete_private_contacts',
	'edit_private_posts'	=> 'edit_private_contacts',
	'read_private_posts'	=> 'read_private_contacts',
	'edit_published_posts'	=> 'edit_published_contacts',
	'publish_posts'			=> 'publish_contacts',
	'delete_published_posts'=> 'delete_published_contacts',
	'edit_posts'			=> 'edit_contacts'	,
	'delete_posts'			=> 'delete_contacts',
	'edit_post' 			=> 'edit_contact',
    'read_post' 			=> 'read_contact',
    'delete_post' 			=> 'delete_contact',
),
'map_meta_cap' => true,
登入後複製

我們的 register_post_type() 函數現在應該如下所示:

/**
* Registers a Custom Post Type called contact
*/
function register_custom_post_type() {
	register_post_type( 'contact', array(
        'labels' => array(
			'name'               => _x( 'Contacts', 'post type general name', 'tuts-crm' ),
			'singular_name'      => _x( 'Contact', 'post type singular name', 'tuts-crm' ),
			'menu_name'          => _x( 'Contacts', 'admin menu', 'tuts-crm' ),
			'name_admin_bar'     => _x( 'Contact', 'add new on admin bar', 'tuts-crm' ),
			'add_new'            => _x( 'Add New', 'contact', 'tuts-crm' ),
			'add_new_item'       => __( 'Add New Contact', 'tuts-crm' ),
			'new_item'           => __( 'New Contact', 'tuts-crm' ),
			'edit_item'          => __( 'Edit Contact', 'tuts-crm' ),
			'view_item'          => __( 'View Contact', 'tuts-crm' ),
			'all_items'          => __( 'All Contacts', 'tuts-crm' ),
			'search_items'       => __( 'Search Contacts', 'tuts-crm' ),
			'parent_item_colon'  => __( 'Parent Contacts:', 'tuts-crm' ),
			'not_found'          => __( 'No contacts found.', 'tuts-crm' ),
			'not_found_in_trash' => __( 'No contacts found in Trash.', 'tuts-crm' ),
		),
        
        // Frontend
        'has_archive' => false,
        'public' => false,
        'publicly_queryable' => false,
        
        // Admin
        'capabilities' => array(
	        'edit_others_posts'		=> 'edit_others_contacts',
			'delete_others_posts'	=> 'delete_others_contacts',
			'delete_private_posts'	=> 'delete_private_contacts',
			'edit_private_posts'	=> 'edit_private_contacts',
			'read_private_posts'	=> 'read_private_contacts',
			'edit_published_posts'	=> 'edit_published_contacts',
			'publish_posts'			=> 'publish_contacts',
			'delete_published_posts'=> 'delete_published_contacts',
			'edit_posts'			=> 'edit_contacts'	,
			'delete_posts'			=> 'delete_contacts',
			'edit_post' 			=> 'edit_contact',
	        'read_post' 			=> 'read_contact',
	        'delete_post' 			=> 'delete_contact',
        ),
        'map_meta_cap' => true,
        'menu_icon' => 'dashicons-businessman',
        'menu_position' => 10,
        'query_var' => true,
        'show_in_menu' => true,
        'show_ui' => true,
        'supports' => array(
        	'title',
        	'author',
        	'comments',
        ),
    ) );	
}
登入後複製

這裡發生了兩件事:

  1. 我們使用 capability 參數定義了自己的功能,並將它們對應到其 Post 等效項。這可確保 WordPress 準確理解這些功能的含義(即 edit_contact 的行為方式與 edit_post 功能相同,只不過它適用於我們的聯絡人自訂貼文類型)。
  2. 我們已告知 WordPress 使用 map_meta_cap 將上述功能對應到 WordPress 的原始功能,以便強制執行。

以任何使用者身分重新載入 WordPress 管理,您將看到我們的聯絡人自訂貼文類型已從 WordPress 管理功能表中消失:

在 WordPress 中构建基本 CRM:利用自定义功能

#發生這種情況是因為我們現在需要告訴 WordPress 哪些角色具有新的聯絡人功能(edit_contactedit_contacts 等)。

建立新的 WordPress 使用者角色,為其指派新的自訂功能

使用 add_role(),我們可以建立一個新的 WordPress 使用者角色並向其指派我們的聯絡人功能。該角色儲存在 WordPress 選項資料中,因此我們只需呼叫該函數一次。

為此,請在我們的插件中的 __construct() 函數末尾添加以下函數:

/**
* Activation hook to register a new Role and assign it our Contact Capabilities
*/
function plugin_activation() {
	
	// Define our custom capabilities
	$customCaps = array(
		'edit_others_contacts'			=> true,
		'delete_others_contacts'		=> true,
		'delete_private_contacts'		=> true,
		'edit_private_contacts'			=> true,
		'read_private_contacts'			=> true,
		'edit_published_contacts'		=> true,
		'publish_contacts'			=> true,
		'delete_published_contacts'		=> true,
		'edit_contacts'				=> true,
		'delete_contacts'			=> true,
		'edit_contact'				=> true,
		'read_contact'				=> true,
		'delete_contact'			=> true,
		'read'					=> true,
	);
	
	// Create our CRM role and assign the custom capabilities to it
	add_role( 'crm', __( 'CRM', 'tuts-crm'), $customCaps );
	
}
登入後複製

此功能將向 WordPress 新增一個名為 CRM 的新角色。指派給此角色的使用者只能存取聯絡人功能。因此,他們只能訪問聯絡人自訂貼文類型。

請注意,我們也為此角色指派了 read 功能。這是允許使用者編輯其個人資料(姓名、密碼等)所必需的。我們需要允許用戶執行此操作,因為當他們登入時,WordPress 會自動將他們重新導向到個人資料畫面。

如果我們沒有指派 read 功能,使用者登入時會發生以下情況:

在 WordPress 中构建基本 CRM:利用自定义功能

要執行我們的 plugin_activation() 函數一次,我們將以下程式碼加入到外掛程式檔案的末尾:

register_activation_hook( __FILE__, array( &$wpTutsCRM, 'plugin_activation' ) );
登入後複製

這告訴 WordPress,在啟動外掛時,它需要呼叫 WPTutsCRM 類別中的 plugin_activation() 函數。

建立/編輯 WordPress 用戶,將他們指派給新的聯絡人角色

接下來,停用並重新啟用您的插件,然後導覽至 WordPress 管理介面中的使用者 > 新增的

如果一切順利,您將看到新的 CRM 角色出現在下拉清單中:

在 WordPress 中构建基本 CRM:利用自定义功能

讓我們繼續建立一個名為 crm 的新用戶,並以該新用戶身分登入。我們現在應該看到我們的聯絡人,其中儀表板個人資料是唯一的其他選單選項:

在 WordPress 中构建基本 CRM:利用自定义功能

#

为其他角色分配自定义功能

如果我们注销并以 WordPress 管理员、编辑或作者身份重新登录,您会发现 WordPress 管理菜单中仍然缺少联系人自定义帖子类型:

在 WordPress 中构建基本 CRM:利用自定义功能

这个是因为我们只分配了 CRM 角色我们的联系人自定义帖子类型的功能。因此,所有其他用户角色仍然无权访问此帖子类型。

To fix this, let’s assign the Custom Capabilities to the Administrator and Editor Roles by adding the following code to the end of the plugin_activation() function:

// Add custom capabilities to Admin and Editor Roles
$roles = array( 'administrator', 'editor' );
foreach ( $roles as $roleName ) {
	// Get role
	$role = get_role( $roleName );
	
	// Check role exists
	if ( is_null( $role) ) {
		continue;
	}
	
	// Iterate through our custom capabilities, adding them
	// to this role if they are enabled
	foreach ( $customCaps as $capability => $enabled ) {
		if ( $enabled ) {
			// Add capability
			$role->add_cap( $capability );
		}
	}
}
登入後複製

在这里,我们迭代要为其分配自定义功能的角色,检查该角色是否存在。如果是,我们就会迭代之前定义的自定义功能,将它们添加到角色中。

您会注意到我们没有向作者角色添加任何自定义功能;这是因为我们不想分配所有功能,因为作者角色传统上只允许对该用户自己的帖子进行写入访问。

让我们通过为作者角色添加一些功能来继续构建我们的 plugin_activation() 函数:

// Add some of our custom capabilities to the Author Role
$role = get_role( 'author' );
$role->add_cap( 'edit_contact' );
$role->add_cap( 'edit_contacts' );
$role->add_cap( 'publish_contacts' );
$role->add_cap( 'read_contact' );
$role->add_cap( 'delete_contact' );
unset( $role );
登入後複製

我们的整个函数现在应该如下所示:

/**
* Activation hook to register a new Role and assign it our Contact Capabilities
*/
function plugin_activation() {
	
	// Define our custom capabilities
	$customCaps = array(
		'edit_others_contacts'			=> true,
		'delete_others_contacts'		=> true,
		'delete_private_contacts'		=> true,
		'edit_private_contacts'			=> true,
		'read_private_contacts'			=> true,
		'edit_published_contacts'		=> true,
		'publish_contacts'				=> true,
		'delete_published_contacts'		=> true,
		'edit_contacts'					=> true,
		'delete_contacts'				=> true,
		'edit_contact'					=> true,
		'read_contact'					=> true,
		'delete_contact'				=> true,
		'read'							=> true,
	);
	
	// Create our CRM role and assign the custom capabilities to it
	add_role( 'crm', __( 'CRM', 'tuts-crm'), $customCaps );
	
	// Add custom capabilities to Admin and Editor Roles
	$roles = array( 'administrator', 'editor' );
	foreach ( $roles as $roleName ) {
		// Get role
		$role = get_role( $roleName );
		
		// Check role exists
		if ( is_null( $role) ) {
			continue;
		}
		
		// Iterate through our custom capabilities, adding them
		// to this role if they are enabled
		foreach ( $customCaps as $capability => $enabled ) {
			if ( $enabled ) {
				// Add capability
				$role->add_cap( $capability );
			}
		}
	}
			
	// Add some of our custom capabilities to the Author Role
	$role = get_role( 'author' );
	$role->add_cap( 'edit_contact' );
	$role->add_cap( 'edit_contacts' );
	$role->add_cap( 'publish_contacts' );
	$role->add_cap( 'read_contact' );
	$role->add_cap( 'delete_contact' );
	unset( $role );
	
}
登入後複製

以管理员、编辑或作者身份登录现在将在 WordPress 管理菜单中显示“联系人”选项:

在 WordPress 中构建基本 CRM:利用自定义功能

清洁提升我们的角色

如果 WordPress 管理员停用我们的插件,CRM 角色将保留在 WordPress 中。但是,鉴于没有其他插件或 WordPress 的一部分使用此角色,因此它是多余的 - 因此我们需要确保在停用我们的插件时删除 CRM 角色。

为此,请在 plugin_activation() 函数下方添加以下函数:

/**
* Deactivation hook to unregister our existing Contacts Role
*/
function plugin_deactivation() {
	
	remove_role( 'crm' );
	
}
登入後複製

与我们在插件激活时使用 register_activation_hook() 函数的方式相同,当我们的插件停用时,我们可以使用 register_deactivation_hook() 函数。让我们在 register_activation_hook 调用下面添加以下内容:

register_deactivation_hook( __FILE__, array( &$wpTutsCRM, 'plugin_deactivation' ) );
登入後複製

停用我们的插件后,我们的 CRM 角色将不再可用。

摘要

我们已经在 WordPress 中成功创建了一个简单的 CRM 系统,探索使用自定义帖子类型、帖子元字段和第三方插件集成来存储有关我们的客户和潜在客户的信息。

本教程还涵盖了 WordPress 的一些更高级的方面,包括通过 WP_List_Table 列显示高级自定义字段数据、过滤我们的帖子查询以搜索我们的高级自定义字段数据,以及通过角色和功能管理用户访问以限制访问仅限我们的 CRM 系统。

以上是在 WordPress 中建立基本 CRM:利用自訂功能的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1669
14
CakePHP 教程
1428
52
Laravel 教程
1329
25
PHP教程
1273
29
C# 教程
1256
24
說明PHP中的安全密碼散列(例如,password_hash,password_verify)。為什麼不使用MD5或SHA1? 說明PHP中的安全密碼散列(例如,password_hash,password_verify)。為什麼不使用MD5或SHA1? Apr 17, 2025 am 12:06 AM

在PHP中,應使用password_hash和password_verify函數實現安全的密碼哈希處理,不應使用MD5或SHA1。1)password_hash生成包含鹽值的哈希,增強安全性。 2)password_verify驗證密碼,通過比較哈希值確保安全。 3)MD5和SHA1易受攻擊且缺乏鹽值,不適合現代密碼安全。

PHP和Python:比較兩種流行的編程語言 PHP和Python:比較兩種流行的編程語言 Apr 14, 2025 am 12:13 AM

PHP和Python各有優勢,選擇依據項目需求。 1.PHP適合web開發,尤其快速開發和維護網站。 2.Python適用於數據科學、機器學習和人工智能,語法簡潔,適合初學者。

PHP行動:現實世界中的示例和應用程序 PHP行動:現實世界中的示例和應用程序 Apr 14, 2025 am 12:19 AM

PHP在電子商務、內容管理系統和API開發中廣泛應用。 1)電子商務:用於購物車功能和支付處理。 2)內容管理系統:用於動態內容生成和用戶管理。 3)API開發:用於RESTfulAPI開發和API安全性。通過性能優化和最佳實踐,PHP應用的效率和可維護性得以提升。

PHP類型提示如何起作用,包括標量類型,返回類型,聯合類型和無效類型? PHP類型提示如何起作用,包括標量類型,返回類型,聯合類型和無效類型? Apr 17, 2025 am 12:25 AM

PHP類型提示提升代碼質量和可讀性。 1)標量類型提示:自PHP7.0起,允許在函數參數中指定基本數據類型,如int、float等。 2)返回類型提示:確保函數返回值類型的一致性。 3)聯合類型提示:自PHP8.0起,允許在函數參數或返回值中指定多個類型。 4)可空類型提示:允許包含null值,處理可能返回空值的函數。

PHP的持久相關性:它還活著嗎? PHP的持久相關性:它還活著嗎? Apr 14, 2025 am 12:12 AM

PHP仍然具有活力,其在現代編程領域中依然佔據重要地位。 1)PHP的簡單易學和強大社區支持使其在Web開發中廣泛應用;2)其靈活性和穩定性使其在處理Web表單、數據庫操作和文件處理等方面表現出色;3)PHP不斷進化和優化,適用於初學者和經驗豐富的開發者。

PHP和Python:解釋了不同的範例 PHP和Python:解釋了不同的範例 Apr 18, 2025 am 12:26 AM

PHP主要是過程式編程,但也支持面向對象編程(OOP);Python支持多種範式,包括OOP、函數式和過程式編程。 PHP適合web開發,Python適用於多種應用,如數據分析和機器學習。

您如何防止PHP中的SQL注入? (準備的陳述,PDO) 您如何防止PHP中的SQL注入? (準備的陳述,PDO) Apr 15, 2025 am 12:15 AM

在PHP中使用預處理語句和PDO可以有效防範SQL注入攻擊。 1)使用PDO連接數據庫並設置錯誤模式。 2)通過prepare方法創建預處理語句,使用佔位符和execute方法傳遞數據。 3)處理查詢結果並確保代碼的安全性和性能。

PHP和Python:代碼示例和比較 PHP和Python:代碼示例和比較 Apr 15, 2025 am 12:07 AM

PHP和Python各有優劣,選擇取決於項目需求和個人偏好。 1.PHP適合快速開發和維護大型Web應用。 2.Python在數據科學和機器學習領域佔據主導地位。

See all articles