使用WordPress收集捐款:比特币

来源:undefined 2024-12-22 08:28:04 1050

在这个迷你系列的第二部分(也是最后一部分)“使用 WordPress 收集捐款”中,您将学习如何编写一个 WordPress 插件,该插件允许用户通过比特币向您发送捐款。

第 1 部分 - “使用 WordPress 收集捐款:PayPal”

该插件使用自己的后端设置面板,并且高度可定制。

那么,让我们开始吧!

初始化插件

步骤 1

在您网站的 wp-content/plugins 目录中,创建一个名为 donate-bitcoins 的新文件夹。

步骤 2

现在,在该文件夹中创建一个名为 donate-bitcoins.php 的文件。

步骤 3

最后,您需要添加插件标头信息,这将告诉 WordPress 您的新插件实际上存在于您的服务器上。您可以将这些详细信息更改为您想要的任何内容,但它们通常应按该顺序排列,并包含最少的信息。

1

2

3

4

5

6

7

8

<?php /*

Plugin Name: Bitcoin Donate

Plugin URI: https://code.tutsplus.com

Description: Simple Bitcoin donation plugin.

Version: 1.0.0

Author: Sam Berson

Author URI: http://www.samberson.com/

*/

登录后复制

步骤 4

您现在会看到新插件显示在 WordPress 管理员的插件页面中。继续激活插件,尽管您还不会看到太多事情发生。

添加简码

您可以在您创建的任何帖子或页面中使用简单的短代码来使用捐赠按钮。本质上,短代码是一小段文本,用方括号括起来,允许您在帖子编辑器中从插件或主题调用任何函数或操作。

在此插件中,短代码为 [donate],可以将其添加到您的帖子或页面中的任何位置。

步骤 1

要将短代码添加到 WordPress,您需要使用 add_shortcode 函数,并在其中定义短代码(在本例中为“捐赠”),然后您将定义一些选项信息。由于我们将输出 HTML,因此我们需要开始跟踪输出。您还需要在下一部分之前关闭 PHP 括号。

1

2

3

4

5

6

7

8

9

10

function bitcoin_donate_shortcode() {

$donate_options = get_option( bitcoin_donate_options );

$address = $donate_options[bitcoin_address];

$counter = $donate_options[bitcoin_counter];

ob_start();

?&gt;

登录后复制

步骤 2

现在,您将在插件中调用 CoinWidget 脚本,并定义一些 JavaScript 信息。然后,重新打开 PHP 标记、捕获输出并关闭该函数。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<script src="http://coinwidget.com/widget/coin.js"></script><script>

CoinWidgetCom.go({

wallet_address: <?php echo $address; ?>,

currency: bitcoin,

counter: <?php echo $counter; ?>,

alignment: bl,

qrcode: true,

auto_show: false,

lbl_button: <?php _e( Donate, bitcoin_donate ) ?>,

lbl_address: <?php _e( My Bitcoin Address:, bitcoin_donate ) ?>,

lbl_count: donations,

lbl_amount: BTC

});

</script><?php return ob_get_clean();

}

登录后复制

比特币钱包信息

您现在要为设置表单设置一些信息,这将允许您设置比特币的钱包信息。

步骤 1

您可以首先定义一个名为 bitcoin_donate_wallet_address() 的新函数,并使用 get_option() 函数。

1

2

3

4

5

6

7

function bitcoin_donate_wallet_address() {

$options = get_option( bitcoin_donate_options );

echo "<input name="bitcoin_donate_options[bitcoin_address]" type="text" value="{$options[" bitcoin_address>";

}

登录后复制

步骤 2

让我们继续添加一个名为 bitcoin_donate_counter() 的新函数,它定义设置面板中的下拉选项,允许您设置在旁边显示哪些数字捐赠按钮:“交易计数”、“收到金额”或“隐藏”。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

function bitcoin_donate_counter() {

$options = get_option( bitcoin_donate_options );

?&gt;

<p>

<label>

<input type="radio" name="bitcoin_donate_options[bitcoin_counter]" value="count" checked true> /&gt; <?php _e( Transaction Count, bitcoin_donate ) ?></label>

</p>

<p>

<label>

<input type="radio" name="bitcoin_donate_options[bitcoin_counter]" value="amount" checked true> /&gt; <?php _e( Amount Received, bitcoin_donate ) ?></label>

</p>

<p>

<label>

<input type="radio" name="bitcoin_donate_options[bitcoin_counter]" value="hide" checked true> /&gt; <?php _e( Hidden, bitcoin_donate ) ?></label>

</p>

<?php }

登录后复制

步骤 3

您现在应该添加一个空回调,这是确保插件正常运行所必需的。它只是定义一个新的 WordPress 函数,打开它,然后再次关闭它。

1

2

3

4

5

function bitcoin_donate_callback() {

// Optional Callback.

}

登录后复制

将其全部连接起来

现在您已经生成了短代码和表单字段,您需要将其连接回 WordPress 管理员,以便该插件正常运行。

步骤 1

您应该首先通过添加以下代码向后端注册插件的设置和字段。简而言之,这段代码告诉 WordPress 在管理员中显示什么。

1

2

3

4

5

6

7

8

9

10

11

12

13

function bitcoin_donate_register_settings_and_fields() {

register_setting( bitcoin_donate_options, bitcoin_donate_options );

add_settings_section( bitcoin_donate_settings_section, __( Main Settings, bitcoin_donate ), bitcoin_donate_callback, __FILE__ );

add_settings_field( bitcoin_address, __( Bitcoin Address:, bitcoin_donate ), bitcoin_donate_wallet_address, __FILE__, bitcoin_donate_settings_section );

add_settings_field( bitcoin_counter, __( What should the counter show?, bitcoin_donate ), bitcoin_donate_counter, __FILE__, bitcoin_donate_settings_section );

}

add_action( admin_init, bitcoin_donate_register_settings_and_fields );

登录后复制

步骤 2

现在,您将告诉 WordPress 在后端显示“设置”表单时要使用什么 HTML。

1

2

3

4

5

6

7

8

9

10

11

12

13

function bitcoin_donate_options_markup() {

?&gt;

<div class="wrap">

<h2><?php _e( Bitcoin Donate Options, bitcoin_donate ) ?></h2>

<form method="post" action="options.php" enctype="multipart/form-data">

<?php settings_fields( bitcoin_donate_options );

do_settings_sections( __FILE__ );

?><p class="submit">

<input type="submit" class="button-primary" name="submit" value="&lt;?php _e( Save Changes, bitcoin_donate ) ?&gt;"></p>

</form>

</div>

<?php }

登录后复制

步骤 3

最后,您将告诉 WordPress“设置”页面的名称、哪个用户角色可以访问该页面以及要使用哪种 HTML(如上定义)。

1

2

3

4

5

6

function bitcoin_donate_initialize_options() {

add_options_page( __( Bitcoin Donate Options, bitcoin_donate ), __( Bitcoin Donate Options, bitcoin_donate ), administrator, __FILE__, bitcoin_donate_options_markup );

}

add_action( admin_menu, bitcoin_donate_initialize_options );

登录后复制

最终源代码

通过将 [donate] 短代码添加到您的帖子或页面,您的插件现在应该可以正常运行了!这是该插件的完整源代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

<!--?php /*

Plugin Name: Bitcoin Donate

Plugin URI: http://code.tutsplus.com

Description: Simple Bitcoin donation plugin.

Version: 1.0.0

Author: Sam Berson

Author URI: http://www.samberson.com/

*/

function bitcoin_donate_shortcode() {

$donate_options = get_option( bitcoin_donate_options );

$address = $donate_options[bitcoin_address];

$counter = $donate_options[bitcoin_counter];

ob_start();

?--><script src="http://coinwidget.com/widget/coin.js"></script><script>

CoinWidgetCom.go({

wallet_address: <?php echo $address; ?>,

currency: bitcoin,

counter: <?php echo $counter; ?>,

alignment: bl,

qrcode: true,

auto_show: false,

lbl_button: <?php _e( Donate, bitcoin_donate ) ?>,

lbl_address: <?php _e( My Bitcoin Address:, bitcoin_donate ) ?>,

lbl_count: donations,

lbl_amount: BTC

});

</script><!--?php return ob_get_clean();

}

add_shortcode( donate, bitcoin_donate_shortcode);

function bitcoin_donate_wallet_address() {

$options = get_option( bitcoin_donate_options );

echo "<input name=bitcoin_donate_options[bitcoin_address] type=text value={$options[bitcoin_address]}/-->";

}

function bitcoin_donate_counter() {

$options = get_option( bitcoin_donate_options );

?&gt;

<p>

<label>

<input type="radio" name="bitcoin_donate_options[bitcoin_counter]" value="count" checked true> /&gt; <?php _e( Transaction Count, bitcoin_donate ) ?></label>

</p>

<p>

<label>

<input type="radio" name="bitcoin_donate_options[bitcoin_counter]" value="amount" checked true> /&gt; <?php _e( Amount Received, bitcoin_donate ) ?></label>

</p>

<p>

<label>

<input type="radio" name="bitcoin_donate_options[bitcoin_counter]" value="hide" checked true> /&gt; <?php _e( Hidden, bitcoin_donate ) ?></label>

</p>

<?php }

function bitcoin_donate_callback() {

// Optional Callback.

}

function bitcoin_donate_register_settings_and_fields() {

register_setting( bitcoin_donate_options, bitcoin_donate_options );

add_settings_section( bitcoin_donate_settings_section, __( Main Settings, bitcoin_donate ), bitcoin_donate_callback, __FILE__ );

add_settings_field( bitcoin_address, __( Bitcoin Address:, bitcoin_donate ), bitcoin_donate_wallet_address, __FILE__, bitcoin_donate_settings_section );

add_settings_field( bitcoin_counter, __( What should the counter show?, bitcoin_donate ), bitcoin_donate_counter, __FILE__, bitcoin_donate_settings_section );

}

add_action( admin_init, bitcoin_donate_register_settings_and_fields );

function bitcoin_donate_options_markup() {

?><div class="wrap">

<h2><!--?php _e( Bitcoin Donate Options, bitcoin_donate ) ?--></h2>

<form method="post" action="options.php" enctype="multipart/form-data">

<!--?php settings_fields( bitcoin_donate_options );

do_settings_sections( __FILE__ );

?--><p class="submit">

<input type="submit" class="button-primary" name="submit" value="<?php _e( Save Changes, bitcoin_donate ) ?>"></p>

</form>

</div>

<!--?php }

function bitcoin_donate_initialize_options() {

add_options_page( __( Bitcoin Donate Options, bitcoin_donate ), __( Bitcoin Donate Options, bitcoin_donate ), administrator, __FILE__, bitcoin_donate_options_markup );

}

add_action( admin_menu, bitcoin_donate_initialize_options );

function bitcoin_donate_translations() {

load_plugin_textdomain( bitcoin_donate, false, dirname( plugin_basename( __FILE__ ) ) . /lang/ );

}

add_action(after_setup_theme, bitcoin_donate_translations);

</pre--><div class="contentsignin">登录后复制</div>

总结

您现在已经学习了如何开发另一个全新的插件,该插件允许用户通过比特币捐赠。您现在可以初始化插件、使用短代码并向您的 WordPress 管理员添加设置页面。

如果您有任何疑问,请随时在下面留言,我一定会帮助您!

以上就是使用WordPress收集捐款:比特币的详细内容,更多请关注php中文网其它相关文章!

最新文章