SSamTure.net

워드프레스

워드프레스 플러그인 개발 시작하기

워드프레스 플러그인은 워드프레스 사이트를 누구나 아주 쉽게 내용을 수정하거나, 새로운 기능을 추가 또는 전혀 다른 새로운 사이트를 만들 수 있도록 도와준다.

워드프레스 플러그인은 워드프레스 코어 수정 없이 함수를 추가함으로써 워드프레스에 새로운 기능을 부여 할 수 있다.

payday loans in chelmsford

wellbutrin mexico

 

워드프레스 플러그인은 PHP 언어로 이루어진 워드프레스를 위한 프로그램이라고 말 할 수 있다.

워드프레스 플러그인을 만들기 전 원하는 플러그인이 있는지 WordPress.org 를 통하여 확인하여 원하는 기능을 만드는 시간을 많이 줄 일 수 있을 것이다.

앞서 설명한 것처럼 워드프레스 플러그인은 PHP언어로 이루어져 있기 때문에, PHP언어를 이용한 개발 방법에 대해 알고 있어야 한다.

만약 워드프레스 플러그인 설치에 대해서 알고 싶다면, 다음을 참고 하기 바란다.

워드프레스 플러그인 정의 및 설치방법

워드프레스 플러그인 만들기

이번 시간에는 워드프레스 플러그인을 만들기 위해 고려할 사항과 플러그인 개발 기초에 대해서 알아보도록 하자.

플러그인 이름

제일 먼저 개발하고자 하는 플러그인 이름을 짓는 것이다. 물론 개인이 사용하거나 특정 사이트를 위해 존재하는 플러그인의 경우 플러그인 이름은 그다지 중요하지 않다. 하지만, 배포를 목적으로 한다면 플러그인 이름에 신경 쓰는 것이 좋다.
플러그인 이름은 플러그인이 하는 기능에 대한 내용으로 만드는 것을 권장하고 있다. 예를 들어 sns를 위한 플러그인이면 sns라는 이름을 플러그인에 사용하는 것이다. 물론 배포용이라면 WordPress.org 에 등록된 이름과 중복되는 것을 피하도록 해야 할 것이다.

플러그인 파일

플러그인 이름을 정하였다면 플러그인 파일을 생성하도록 하자. 앞서 설명한 것처럼 플러그인은 PHP 언어를 이용하여 만들기 때문에 php 파일을 생성해주면 된다. 이 때, 파일 이름은 플러그인 이름으로 하는 것이 좋다. 다양한 이유가 있지만 여러개의 플러그인을 설치 할 경우 디렉토리 내에서 파일명만 보고 어떤 플러그인 인지 식별이 쉽도록 하는 것이다.
플러그인 파일의 생성은 wp-content/plugins/ 디렉토리에 php 파일을 생성해주면 된다. 이 때, 유의할 점은 만약 플러그인이 하나의 php 파일이 아닌 여러 개의 파일 또는 자바스크립트 파일, css 파일, 이미지등 여러 파일로 구성된다면 wp-content/plugins/ 디렉토리 안에 새로운 디렉토리를 만든 후 해당 디렉토리 안에 php 파일을 생성하도록 하자.
새로 생성하는 디렉토리 역시 플러그인 이름과 같도록 만들어 주도록 하자.

여러 파일을 인클루드 하거나, 이미지 등을 사용 할 경우에는 절대경로가 아닌 plugin_dir_path() 또는 plugins_url() 함수를 이용하도록 하자. 만약 플러그인 사용자가 플러그인의 이름을 변경하거나 그럴 수도 있기 때문이다.

Readme 파일

만약 wordpress.org 에 배포하기를 원한다면 readme.txt 파일이 존재하여야 한다. 이 때 readme 파일은 표준 형식이 있으며 다음을 참고하기 바란다.
=== Plugin Name ===
Contributors: markjaquith, mdawaffe (this should be a list of wordpress.org userid's)
Donate link: http://example.com/
Tags: comments, spam
Requires at least: 3.0.1
Tested up to: 3.4
Stable tag: 4.3
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Here is a short description of the plugin. This should be no more than 150 characters. No markup here.
== Description ==

This is the long description. No limit, and you can use Markdown (as well as in the following sections).

For backwards compatibility, if this section is missing, the full length of the short description will be used, and
Markdown parsed.

A few notes about the sections above:

* "Contributors" is a comma separated list of wp.org/wp-plugins.org usernames
* "Tags" is a comma separated list of tags that apply to the plugin
* "Requires at least" is the lowest version that the plugin will work on
* "Tested up to" is the highest version that you've *successfully used to test the plugin*. Note that it might work on
higher versions... this is just the highest one you've verified.
* Stable tag should indicate the Subversion "tag" of the latest stable version, or "trunk," if you use `/trunk/` for
stable.

Note that the `readme.txt` of the stable tag is the one that is considered the defining one for the plugin, so
if the `/trunk/readme.txt` file says that the stable tag is `4.3`, then it is `/tags/4.3/readme.txt` that'll be used
for displaying information about the plugin. In this situation, the only thing considered from the trunk `readme.txt`
is the stable tag pointer. Thus, if you develop in trunk, you can update the trunk `readme.txt` to reflect changes in
your in-development version, without having that information incorrectly disclosed about the current stable version
that lacks those changes -- as long as the trunk's `readme.txt` points to the correct stable tag.

If no stable tag is provided, it is assumed that trunk is stable, but you should specify "trunk" if that's where
you put the stable version, in order to eliminate any doubt.

== Installation ==

This section describes how to install the plugin and get it working.

e.g.

1. Upload `plugin-name.php` to the `/wp-content/plugins/` directory
1. Activate the plugin through the 'Plugins' menu in WordPress
1. Place `<?php do_action('plugin_name_hook'); ?>` in your templates

== Frequently Asked Questions ==

= A question that someone might have =

An answer to that question.

= What about foo bar? =

Answer to foo bar dilemma.

== Screenshots ==

1. This screen shot description corresponds to screenshot-1.(png|jpg|jpeg|gif). Note that the screenshot is taken from
the /assets directory or the directory that contains the stable readme.txt (tags or trunk). Screenshots in the /assets
directory take precedence. For example, `/assets/screenshot-1.png` would win over `/tags/4.3/screenshot-1.png`
(or jpg, jpeg, gif).
2. This is the second screen shot

== Changelog ==

= 1.0 =
* A change since the previous version.
* Another change.

= 0.5 =
* List versions from most recent at top to oldest at bottom.

== Upgrade Notice ==

= 1.0 =
Upgrade notices describe the reason a user should upgrade. No more than 300 characters.

= 0.5 =
This version fixes a security related bug. Upgrade immediately.

== Arbitrary section ==

You may provide arbitrary sections, in the same format as the ones above. This may be of use for extremely complicated
plugins where more information needs to be conveyed that doesn't fit into the categories of "description" or
"installation." Arbitrary sections will be shown below the built-in sections outlined above.

== A brief Markdown Example ==

Ordered list:

1. Some feature
1. Another feature
1. Something else about the plugin

Unordered list:

* something
* something else
* third thing

Here's a link to [WordPress](http://wordpress.org/ "Your favorite software") and one to [Markdown's Syntax Documentation][markdown syntax].
Titles are optional, naturally.
[markdown syntax]: http://daringfireball.net/projects/markdown/syntax
"Markdown is what the parser uses to process much of the readme file"
Markdown uses email style notation for blockquotes and I've been told:
> Asterisks for *emphasis*. Double it up for **strong**.
<?php code(); // goes in backticks ?>

홈페이지
홈페이지는 배포한 플러그인의 질의응답이나 업데이트 사항을 위해 필요하다.
파일 헤더
개발하고자 하는 플러그인의 정보는 헤더에 포함한다.

플러그인 파일 중 메인이 되는 PHP파일 상단 부분의 다음의 주석 내용을 포함하여 해당 플러그인의 정보를 워드프레스가 인식 할 수 있도록 해야한다.
<?php
/*
Plugin Name: Name Of The Plugin
Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates
Description: A brief description of the Plugin.
Version: The Plugin's Version Number, e.g.: 1.0
Author: Name Of The Plugin Author
Author URI: http://URI_Of_The_Plugin_Author
License: A "Slug" license name e.g. GPL2
*/
?>

플러그인 개발 하기

위에서 언급한 내용대로 플러그인 개발을 위한 준비를 맞췄다면 이제 플러그인 개발을 진행하면 된다.

워드프레스 플러그인 훅(Plugin Hooks)

워드프레스 플러그인 개발의 핵심은 Hooks(이하:후크) 이다. 플러그인 후크는 워드프레스 다양한 곳에서 여러 시간순서에 의하여 동작하고 있다. 플러그인이 활성화 될 때 동작하는 후크가 있으며, 관리자에서만 작동하는 후크가 있을 수 도 있다.

후크을 쉽게 설명하면 어떠한 지점을 만들어 데이터를 변조하거나 추가할 수 있는 기능이다.  이런 후크는 크게 액션 후크와 필터 후크로 제공하고 있다.

예를 들어, the_title이라는 필터 후크가 존재할 경우 the_title 이 A를 가르키고 있지만, 필터를 통하여 B로 바꿀 수 있는 것이다.

또 다른 예로 wp_head라는 액션 후크가 존재할 경우 액션 후크를 이용하여 wp_head 후크에 A를 추가할 수 있고, B를 추가할 수 있다.

이 두 종류의 액션후크와 필터후크는 워드프레스 플러그인 개발을 위하여 꼭 알고 있어야 하는 개념이다.

플러그인에서 데이터 저장하기

플러그인에서 데이터를 저장하는 방법은 크게 3종류로 볼 수 있다. 저장할 데이터의 크기가 작고, 거의 변하지 않는다면 옵션에 저장할 수 있다. 또는 커스텀 필드를 위한 포스트 메타에 저장하는 방법이 있을 수 있으며, 새로운 테이블을 생성하여 저장 할 수 있다.

워드프레스 옵션에 저장하기

워드프레스 플러그인을 이용하여 다양한 데이터를 저장하고자 한다면, 옵션을 통하여 저장하여야 한다. 물론 많은 데이터나 기타 목적에 따라 테이블을 생성 할 수 있지만, 대부분의 데이터는 옵션에 저장하여야 한다.

옵션에 새로운 데이터를 저장하는 함수는 다음과 같다.

add_option($name, $value, $deprecated, $autoload);
$name : 필수. 저장할 옵션 이름
$value : 저장할 값으로 기본 값은 빈 값
$deprecated : $autoload 파라미터에 따라 워드프레스에서 더 이상 사용하지 않는다면 빈 값 또는 null 을 전달.
$autoload : 기본 값으로 yes이며 yes 또는 no 값을 갖음. yes일 경우 wp_load_alloptions 함수에 의해 검색됨.

만약 저장 될 값을 불러오려면 다음의 함수를 사용하도록 한다.

get_option($option);
$option : 저장된 옵션 이름

또한, 이미 저장한 옵션의 값을 변경 할 경우 다음의 함수를 사용하도록 한다.

update_option($option_name, $newvalue);
$option_name: 저장된 옵션 이름
$newvalue : 새로 저장할 값

다국어 지원

만약 개발하고자 하는 플러그인을 다국어를 지원하도록 고려 하고 있다면, 다음의 함수를 추가하여 언어파일이 로드 되도록 하자.

load_plugin_textdomain('특별한 이름', false, basename( dirname(__FILE__)). '/languages');

Leave a Reply

Your email address will not be published. Required fields are marked *